c# datagrid DataRowView 判断是否有某个列

using System;
using System.Data;
using System.Windows.Forms;

public class Example
{
    public void CheckColumnExists(DataRowView rowView, string columnName)
    {
        // 检查 DataRowView 是否为空
        if (rowView == null)
        {
            Console.WriteLine("DataRowView is null.");
            return;
        }

        // 获取 DataTable
        DataTable table = rowView.DataView.Table;

        // 检查是否存在指定的列
        if (table.Columns.Contains(columnName))
        {
            Console.WriteLine($"Column '{columnName}' exists.");
        }
        else
        {
            Console.WriteLine($"Column '{columnName}' does not exist.");
        }
    }
}

// 使用示例
public class Program
{
    public static void Main()
    {
        // 创建一个示例 DataTable
        DataTable table = new DataTable();
        table.Columns.Add("ID", typeof(int));
        table.Columns.Add("Name", typeof(string));

        // 添加一行数据
        DataRow row = table.NewRow();
        row["ID"] = 1;
        row["Name"] = "John Doe";
        table.Rows.Add(row);

        // 创建 DataRowView
        DataRowView rowView = table.DefaultView[0];

        // 检查列是否存在
        Example example = new Example();
        example.CheckColumnExists(rowView, "Name"); // 输出: Column 'Name' exists.
        example.CheckColumnExists(rowView, "Age");  // 输出: Column 'Age' does not exist.
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值