2017-05-08 89 views
1

我使用下面的代碼,但它沒有工作..它沒有顯示與該字段列。如何在gridview中顯示行號

<asp:TemplateField> 
    <ItemTemplate> 
    <%# Container.DataItemIndex + 1 %> 
    </ItemTemplate> 
</asp:TemplateField> 

我在代碼後面添加我的其他列和meaby這是問題嗎?

OleDbDataAdapter sqlArchiveData = new OleDbDataAdapter(sql_archive); 

    DataTable dt = new DataTable(); 
    connProdcc1.Open(); 
    sqlArchiveData.Fill(dt); 

foreach (DataColumn column in dt.Columns) 
     { 
      BoundField field = new BoundField(); 
      field.DataField = column.ColumnName; 
      field.HeaderText = column.ColumnName.Replace("_"," "); 
      field.SortExpression = column.ColumnName; 
      AggregateGridView.Columns.Add(field); 
     } 
AggregateGridView.DataSource = dt; 
     AggregateGridView.DataBind(); 

環己酮是否有任何想法如何做到這一點的其他方式?

我嘗試這個喜歡以下,但它的所有數據後計算我的行數我Fill()DataTable,然後添加到各行的數量

 DataColumn columnIndexRow = new DataColumn(); 
    columnIndexRow.DataType = System.Type.GetType("System.Int32"); 
    columnIndexRow.ColumnName = "id"; 
    dt.Columns.Add(columnIndexRow); 

    for (int i = 0; i < dt.Rows.Count; i++) 
    { 
     // your index is in i 
     var row = dt.NewRow(); 
     row["id"] = i; 
     dt.Rows.Add(row); 
    } 

回答

0

謝謝你們!我只是找出答案。我做一些更新行索引處的單元格和第一列

 for (int i = 0; i < dt.Rows.Count; i++) 
    { 
     dt.Rows[i][0] = i; 
    } 
0

添加行號字段,以數​​據表

OleDbDataAdapter sqlArchiveData = new OleDbDataAdapter(sql_archive); 

DataTable dt = new DataTable(); 
connProdcc1.Open(); 
sqlArchiveData.Fill(dt); 

dt = AutoNumberedTable(dt); 

foreach (DataColumn column in dt.Columns) 
{ 
    BoundField field = new BoundField(); 
    field.DataField = column.ColumnName; 
    field.HeaderText = column.ColumnName.Replace("_"," "); 
    field.SortExpression = column.ColumnName; 
    AggregateGridView.Columns.Add(field); 
} 
AggregateGridView.DataSource = dt; 
AggregateGridView.DataBind(); 


private DataTable AutoNumberedTable(DataTable SourceTable) 
{ 
    DataTable ResultTable = new DataTable(); 
    DataColumn AutoNumberColumn = new DataColumn(); 
    AutoNumberColumn.ColumnName="S.No."; 
    AutoNumberColumn.DataType = typeof(int); 
    AutoNumberColumn.AutoIncrement = true; 
    AutoNumberColumn.AutoIncrementSeed = 1; 
    AutoNumberColumn.AutoIncrementStep = 1; 
    ResultTable.Columns.Add(AutoNumberColumn); 
    ResultTable.Merge(SourceTable); 
    return ResultTable; 
} 

希望這可能有幫助。 :)

注:我還沒有測試過這個代碼。