我有一个包含很少列单元格的gridview。我想要的只是检查空单元格并默认写入null。如果某些列未填充。我想要一个默认的空文本到特定的单元格
我试图创造这样的东西,但它不起作用,因为我不知道我怎么能进一步发展
private void Gridview_Output_CellFormatting_1(object sender, DataGridViewCellFormattingEventArgs e)
{
string _Car = string.Empty;
string _Dealer = string.Empty;
string _Model = string.Empty;
int gridcount = Gridview.Rows.Count;
for (int i = 0; i < gridcount; i++)
{
_Car = "";
_Dealer = "";
_Model = "";
if (Gridview.Rows[i].Cells[1].Value == DBNull.Value)
{
_Car = "Null";
}
else
{
_Car = Gridview.Rows[i].Cells[1].Value.ToString();
}
if (Gridview.Rows[i].Cells[2].Value == DBNull.Value)
{
_Dealer = "Null";
}
else
{
_Dealer = Gridview.Rows[i].Cells[2].Value.ToString();
}
if (Gridview.Rows[i].Cells[4].Value == DBNull.Value)
{
_Model = "Null";
}
else
{
_Model = Gridview.Rows[i].Cells[4].Value.ToString();
}
}
}
答案 0 :(得分:1)
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
if (!dr.IsNewRow)
{
for (int c = 0; c <= dr.Cells.Count - 1; c++)
{
if (dr.Cells[c].Value == null)
{
dr.Cells[c].Value = "NULL TEXT";
}
}
}
}
答案 1 :(得分:0)
您正在寻找DataGridView
的{{1}}事件。
尝试这样的事情:
CellFormatting
如有必要,您还可以使用private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (string.IsNullOrEmpty(e.Value as string))
{
e.Value = "NULL";
e.FormattingApplied = true;
}
}
属性添加列条件。