c#如何获取已检查的datagridview复选框旁边的值?

时间:2018-01-02 14:33:04

标签: c# winforms checkbox datagridview

我有一个带CheckBox列的DataGridView,我的问题是如何获取已选中复选框旁边的数据并将它们分配给数组中的变量?

    private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {            
        foreach (DataGridViewRow row in this.dataGridView1.Rows)
        {
            if (Convert.ToBoolean(this.dataGridView1.Rows[e.RowIndex].Cells["chkBoxColumn"].Value = true) == true)
            {
                MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString());
            }
        }   
    }

反复打印上次检查的项目。

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        for (int i = 0; i < dataGridView1.Rows.Count; i++)

        {
            if (bool.Parse(dataGridView1.Rows[i].Cells["chkBoxColumn"].Value.ToString()) == true)
            {
                MessageBox.Show(dataGridView1.Rows[i].Cells[1].Value.ToString());
            }
        }
    } 

我正在使用它获取空引用异常。

4 个答案:

答案 0 :(得分:1)

问题出在

do {
   decompressedData = try XZArchive.unarchive(archive: data)
} catch let _error as XZError {
   print("decompressed Data XZ error " + _error.localizedDescription)
} catch let _error {
   print("Data error " + _error.localizedDescription)
}

MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString()); 指向已更改的行

它应该是:

e.RowIndex

另请注意,您在第二个示例中遇到错误,因为某些行在复选框列中包含空值。

for (int i = 0; i <= this.dataGridView1.Rows) { var row = this.dataGridView1.Rows[i] if (Convert.ToBoolean(this.dataGridView1.Rows[i].Cells["chkBoxColumn"].Value = true) == true) { MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString()); } } 返回false

Convert.ToBoolean(null)会抛出异常

答案 1 :(得分:1)

我猜是这样的?

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{            
    Stringbuilder text = new StringBuilder();
    foreach (DataGridViewRow row in this.dataGridView1.Rows)
    {
        if (Convert.ToBoolean(row.Cells["chkBoxColumn"].Value = true) == true)
        {
            text.Append(row.Cells[1].Value.ToString());                
        }
    }   

    MessageBox.Show(text.ToString());
}

编辑:问题已从显示值更改为将其保存到数组中,因此我还需要更改我的答案

假设您希望将Cell [1]的所有值保存到数组中 并假设该单元格是整数类型。

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{            
    List<int> list = new List<int>();

    foreach (DataGridViewRow row in this.dataGridView1.Rows)
    {
        if (Convert.ToBoolean(row.Cells["chkBoxColumn"].Value = true) == true)
        {
            list.Add(int.Parse(row.Cells[1].Value.ToString()));                
        }
    }   

    // you now have all values saved into the list
}

答案 2 :(得分:1)

修改
问题发生了变化,因此调整了代码,以便将值放入列表中。

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{            
    List<string> someList = new List<string>();

    foreach (DataGridViewRow row in this.dataGridView1.Rows)
    {
        var cell = row.Cells[e.ColumnIndex] as DataGridViewCheckBoxCell;

        if (Convert.ToBoolean(cell.Value) == true)
        {
            if (cell.State != DataGridViewElementStates.Selected)
            {
                someList.Add(row.Cells[1].Value.ToString();
            }
        }
        else if (cell.State == DataGridViewElementStates.Selected)
        {
            someList.Add(row.Cells[1].Value.ToString();
        }
    }   
}

这可能会为您提供帮助,但是每次用户勾选复选框时您都会弹出一个弹出窗口,并且您将获得与true复选框一样多的弹出窗口。

答案 3 :(得分:1)

您可以点击按钮放置代码并以这种方式查找选中的值:

private void button1_Click(object sender, EventArgs e)
{
    var checkedValues = dataGridView1.Rows.Cast<DataGridViewRow>()
        .Where(row => (bool?)row.Cells["Your CheckBox Column Name"].Value == true)
        .Select(row => string.Format("{0}", row.Cells["The Other Column Name"].Value))
        .ToList();

    MessageBox.Show(string.Join(",", checkedValues));
}