Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法获取索引_C#_Asp.net_Datetime_Datagrid - Fatal编程技术网

C# 无法获取索引

C# 无法获取索引,c#,asp.net,datetime,datagrid,C#,Asp.net,Datetime,Datagrid,我在动态数据网格中有一列,显示日期和时间。我试图只显示日期 DataRowView drRow = (DataRowView)e.Item.DataItem; DataGrid dg = sender as DataGrid; int index = dg.Columns.HeaderIndex("Date"); if (index > 0 && drRow.Row.Table.Columns.Contains("GateIn

我在动态数据网格中有一列,显示日期和时间。我试图只显示日期

DataRowView drRow = (DataRowView)e.Item.DataItem;
        DataGrid dg = sender as DataGrid;
        int index = dg.Columns.HeaderIndex("Date");
        if (index > 0 && drRow.Row.Table.Columns.Contains("GateIn"))
        {
            DateTime dt = Convert.ToDateTime(drRow["GateIn"]);
            e.Item.Cells[index].Text = dt.ToShortDateString();
        }

它找不到索引,因此跳出if语句

尝试通过indexe.g列搜索索引。0表示第一列,1表示第二列等。

您也可以尝试首先从DataGrid提取DataTable。比如:

DataTable dt = DataGrid.DataSource as DataTable

现在您可以检查数据表变量dt。

问题在于headerindex错误。不应该是约会

int index = dg.Columns.HeaderIndex("Gate In");
            if (index > 0 && drRow.Row.Table.Columns.Contains("GateIn"))
            {
                DateTime dt = Convert.ToDateTime(drRow["GateIn"]);
                e.Item.Cells[index].Text = dt.ToShortDateString();
            }