Gridview复选框是指如果未选中复选框,则无法将对象从dbnull强制转换为其他类型C# [英] Gridview checkbox is giving object cannot be cast from dbnull to other types if check box is not checked C#
问题描述
您好我使用以下代码将gridview dataschema和已检查的行复制到一个单独的表。
检查完所有行后,它的工作正常。但是当我在开始时未选中几行时,无法将dbnull转换为其他类型的
对象。
请帮助解决此问题。我需要将唯一检查的行数据放入该变量。
感谢先进...
我的尝试:
var bindingsource =(BindingSource)dgv_summary.DataSource;
DataTable tbl =((DataTable)bindingsource.DataSource).Clone();
var rows = dgv_summary.Rows.OfType< DataGridViewRow>()
.Where(r => Convert.ToBoolean(r.Cells [] .Value))
.Select( r =>((DataRowView)r.DataBoundItem).Row);
foreach(行中的var行)
{
tbl.ImportRow(row);
}
DataTable dt_Cust = tbl.DefaultView.ToTable(true,GL_ACNO);
解决方案
尝试使用方法进行转换
var rows = dgv_summary.Rows.OfType< DataGridViewRow>()
.Where(r => GetBoolValue(r.Cells [] .Value))
。选择(r =>((DataRowView)r.DataBoundItem).Row);
private bool GetBoolValue(对象值){
string val =转换。的ToString(值);
bool returnValue;
Boolean.TryParse(val,out returnValue);
返回returnValue;
}
最有可能是声明:
r = > Convert.ToBoolean(r.Cells [ ]。值)
如果是dbnull则无法将其转换为布尔值,因为它没有隐式值。您需要添加一些代码来忽略带有dbnull条目的行。
Hi im using the following code to copy a gridview dataschema and checked rows to a separate table.
It works very fine when all the rows are checked. but giving an errorobject cannot be cast from dbnull to other typeswhen i left few rows unchecked in the beginning.
Please help to solve this issue. i need to get the only checked row data into that variable.
Thanks in advanced...
What I have tried:
var bindingsource = (BindingSource)dgv_summary.DataSource; DataTable tbl = ((DataTable)bindingsource.DataSource).Clone(); var rows = dgv_summary.Rows.OfType<DataGridViewRow>() .Where(r => Convert.ToBoolean(r.Cells[" "].Value)) .Select(r => ((DataRowView)r.DataBoundItem).Row); foreach (var row in rows) { tbl.ImportRow(row); } DataTable dt_Cust = tbl.DefaultView.ToTable(true, "GL_ACNO");解决方案try casting in a method
var rows = dgv_summary.Rows.OfType<DataGridViewRow>() .Where(r => GetBoolValue( r.Cells[" "].Value)) .Select(r => ((DataRowView)r.DataBoundItem).Row);
private bool GetBoolValue(object value) { string val = Convert.ToString(value); bool returnValue ; Boolean.TryParse(val, out returnValue); return returnValue; }
Most likely it is the statement:
r => Convert.ToBoolean(r.Cells[" "].Value)
If it is dbnull then it cannot be converted to boolean as it has no implicit value. You need to add some code to ignore rows with dbnull entries.
这篇关于Gridview复选框是指如果未选中复选框,则无法将对象从dbnull强制转换为其他类型C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!