DataGridView的CheckBox是否按行检查或取消搜索 [英] CheckBox of DataGridView is it checked or uncecked search by row
本文介绍了DataGridView的CheckBox是否按行检查或取消搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要搜索的DataGridView复选框是选中还是未选中.如果选中,则我要使用该行.这是我的问题.
我的代码:
I want to search is the DataGridView checkbox checked or unchecked. If checked than I want to use that row. This is my problem.
My Code :
private void PracticalSubjectLoad()
{
try
{
foreach (DataGridViewRow row in gdAddNewSubject.Rows)
{
DataGridViewCheckBoxCell CbxCell = (DataGridViewCheckBoxCell)row.Cells[1];
if(Convert.ToBoolean(CbxCell.Value) == true)
gdPractical.Rows.Add(row.Cells[1].Value.ToString());
//if (Convert.ToBoolean((DataGridViewCheckBoxCell)row.Cells[1].Value) == true)
//{
// gdPractical.Rows.Add(row.Cells[1].Value.ToString());
//}
}
//for (int i = 0; i < gdAddNewSubject.Rows.Count; i++)
//{
// //if (Convert.ToByte(db.dataSet.Tables["subject"].Rows[i][1].ToString()) == 1)
// //{
// // ((DataGridViewCheckBoxCell)gdAddNewSubject.Rows[0].Cells[1]).Value = true;
// //}
// MessageBox.Show(gdAddNewSubject.Rows.Count.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
错误:无法将对象从DBNull强制转换为其他类型.
其他信息此代码仅在检查我的第一行时有效.
请告诉我如何解决此问题
Error: Object cannot be cast from DBNull to other types.
Additional information this code is working only when my 1st-row is checked.
Please tell me how can I solve this problem
推荐答案
DataGridViewCheckBoxCell CbxCell = row.Cells[1] as DataGridViewCheckBoxCell;
if (CbxCell!=null && !DBNull.Value.Equals(CbxCell.Value) && (bool)CbxCell.Value == true)
{
gdPractical.Rows.Add(CbxCell.Value.ToString());
}
在此示例中,我假设这是一个具有三列的DataGridView.
名|姓氏|已注册
以下代码将注册客户添加到列表中.
In this sample I assume that here it is a DataGridView with three columns.
FirstName | LastName | IsRegistered
The Codes below, adds the Registered Customer to a List<t>.
List<customer> list = new List<customer>();
foreach (DataGridViewRow row in this.GridView.Rows)
{
// if a cell has never choosed so it is null
if ((row.Cells["IsRegistered"].Value) == null)
continue;
if (((bool)row.Cells["IsRegistered"].Value == true))
{
list.Add(new Customer() {
FirstName = (string)row.Cells["FirstName"].Value,
LastName = (string)row.Cells["LastName"].Value
};
}
}
让我知道您是否还有其他问题.
Let me know if you have any problem else.
这篇关于DataGridView的CheckBox是否按行检查或取消搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文