【问题标题】:Can't get index无法获取索引
【发布时间】:2015-09-02 10:51:41
【问题描述】:

我在显示日期和时间的动态 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"))
        {
            DateTime dt = Convert.ToDateTime(drRow["GateIn"]);
            e.Item.Cells[index].Text = dt.ToShortDateString();
        }

它没有找到索引,所以它跳出了 if 语句。

【问题讨论】:

    标签: c# asp.net datetime datagrid


    【解决方案1】:

    尝试按列索引搜索索引(例如,0 表示第一列,1 表示第二列等)

    【讨论】:

      【解决方案2】:

      您也可以尝试先从 DataGrid 中提取 DataTable。喜欢:

      DataTable dt = DataGrid.DataSource as DataTable
      

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

      【讨论】:

        【解决方案3】:

        问题是 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();
                    }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-05-24
          • 1970-01-01
          • 1970-01-01
          • 2023-03-30
          • 1970-01-01
          相关资源
          最近更新 更多