不能隐式地将类型字符串转换为System.Windows.Forms.Datagridviewcell c#
我想做一些类似的事情,如果从下拉框中有选择,它应该对Datagridview行进行颜色更改。不能隐式地将类型字符串转换为System.Windows.Forms.Datagridviewcell c#
我已经使用开关盒,没有改变任何东西,因此我想用另一种方法。代码如下所示
private void RowsColor()
{
try
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1 != null)
{
if (dataGridView1.Rows != null)
{
if (dataGridView1.Rows[i].Cells != null)
{
if (dataGridView1.Rows[i].Cells[11] != null)
{
//string val = dataGridView1.Rows[i].Cells[11].Value.ToString();
var val = dataGridView1.Rows[i].Cells[11];
if (val = "Confirm Appointment")
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
}
else if (val = "Reschedule")
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
}
}
}
}
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
现在我得到这样的事情作为一个错误
不能字符串类型隐式转换为 System.Windows.Forms.Datagridviewcell
这个新,我错过了什么?
Decomment
string val = "";
try{
val = dataGridView1.Rows[i].Cells[11].Value.ToString();
}
catch(NullReferenceException ex){
val = "Not yet set"
}
和
if (val.Equals("Confirm Appointment"))
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
}
else if (val.Equals("Reschedule"))
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
}
会抛出字符串异常val = dataGridView1.Rows [i] .Cells [1] .Cells [11] .Value.ToString 11] .Value.ToString(); – Thomas
哪个例外? – farbiondriven
NullreferenceException对象引用未设置为对象的实例,整天都在与它对抗! – Thomas
在哪一行? – farbiondriven
@farbiondriven if(val =“Confirm Appointment”)if else(val =“Reschedule”) – Thomas
@Thomas'val'是符合时间确定的'Datagridviewcell'类型,但您将其与字符串 – Niladri