string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
connExcel.Close();
//Read Data from First Sheet
connExcel.Open();
cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
oda.SelectCommand = cmdExcel;
oda.Fill(dt);
connExcel.Close();
GridView1.Caption = Path.GetFileName(FilePath);
GridView1.DataSource = dt;
GridView1.DataBind();
我正在导入Excel工作表但是代替空值我希望数据集显示为null而不是任何内容。
答案 0 :(得分:2)
您需要向RowDataBound事件添加方法,然后迭代每个单元格。如果其内容为null
,我们会将其设置为"null"
:
void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
for(int i = 0; i < GridView1.Columns.Count, i++)
{
if(String.IsNullOrEmpty(e.Row.Cells[i].Text))
{
e.Row.Cells[i].Text = "null";
}
}
}
如果你想在上述事件之外进行,你需要首先遍历每一行,然后检查Tim Schmelter here演示的单元格:
foreach(GridViewRow row in GridView1.Rows)
{
for(int i = 0; i < GridView1.Columns.Count, i++)
{
if(String.IsNullOrempty(row.Cells[i].Text))
{
row.Cells[i].Text = "null";
}
}
}