如何在Winform应用程序中获取DataGridView特定单元格的值

3

我有一个dataGridView,其中第一列包含复选框。现在根据我的要求,在另一个按钮单击事件中,我必须获取已单击复选框所在行的Employee No列的值。还需要获取选择多个复选框的情况下这个列的值。

以下是我的代码...

    private void btn_load_Click(object sender, EventArgs e)
    { 
        DataTable dt = new DataTable();
        dt.Columns.Add("Select", System.Type.GetType("System.Boolean"));
        dt.Columns.Add("Employee No");
        dt.Columns.Add("Employee Name");
        dt.Columns.Add("Join Date");

        DataRow dr;

        for (int i = 0; i <= 10; i++)
        {
            dr = dt.NewRow();
            dr["Select"] = false;
            dr["Employee No"] = 1000 + i;
            dr["Employee Name"] = "Employee " + i;
            dr["Join Date"] = DateTime.Now.ToString("dd/MM/yyyy");

            dt.Rows.Add(dr);
        }

        dataGridView1.AllowUserToAddRows = true;
        dataGridView1.AllowUserToDeleteRows = true;
        dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

        dataGridView1.DataSource = dt; 
    } 

    private void btn_Click(object sender, EventArgs e)
    {
        //I need the Employee Id values here
    } 

请帮助我。提前致谢。


1
使用 DataGridView_CellContentClick 事件捕获员工 ID 值。 - Pranav1688
私有子 DataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView.CellContentClick 如果 DataGridView.Rows(DataGridView.CurrentCell.RowIndex).Cells(0).Value = True Then '做某事 End If End Sub - Pranav1688
3个回答

3
您可以使用DataSource属性:
    private void btn_Click(object sender, EventArgs e)
    { 
       int[] employeeIds = (dataGridView1.DataSource as DataTable).Rows
           .Cast<DataRow>()
           .Where(r => (bool)r["Select"])
           .Select(r => Convert.ToInt32(r["Employee No"]))
           .ToArray();
    }

并使用System.Linq命名空间。


1
因为您已将DataTable绑定到网格的DataSource,所以您可以将dt设为类变量并使用它来检查所选内容。
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private DataTable dt;

    private void btn_load_Click(object sender, EventArgs e)
    {
        dt = new DataTable();
        dt.Columns.Add("Select", System.Type.GetType("System.Boolean"));
        dt.Columns.Add("Employee No");
        dt.Columns.Add("Employee Name");
        dt.Columns.Add("Join Date");

        DataRow dr;

        for (int i = 0; i <= 10; i++)
        {
            dr = dt.NewRow();
            dr["Select"] = false;
            dr["Employee No"] = 1000 + i;
            dr["Employee Name"] = "Employee " + i;
            dr["Join Date"] = DateTime.Now.ToString("dd/MM/yyyy");

            dt.Rows.Add(dr);
        }

        dataGridView1.AllowUserToAddRows = true;
        dataGridView1.AllowUserToDeleteRows = true;
        dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

        dataGridView1.DataSource = dt;
    }

    private void btn_Click(object sender, EventArgs e)
    {
        //I need the Employee Id values here
        foreach (DataRow row in dt.Rows)
        {
            if ((bool)row["Select"] == true)
            {

            }
        }
    } 
}

1

假设您在表单类中声明了一个全局变量:

List<int> empIDs = new List<int> empIDs();

在你的点击事件中,你可以写下以下内容:

private void btn_Click(object sender, EventArgs e)
{
    empIDs.Clear();
    foreach(DataGridViewRow r in dgv.Rows)
    {
        DataGridViewCheckBoxCell c = r.Cells["Select"] as DataGridViewCheckBoxCell;
        if(Convert.ToBoolean(c.Value))
            empIDs.Add(Convert.ToInt32(r.Cells["Employee No"].Value));        
    }
}    

在点击事件结束时,全局变量将被填充为其SELECT单元格被点击的员工的ID。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接