2017-08-16 59 views
-1

我打算創建一個4列10行的gridview板。我想使用循環。我不知道如何處理CS代碼。我是一名初學者,我正在練習作爲一個例子。請幫助。創建一個ASP.net GridView板(沒有服務器連接)

的.aspx

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" 
          AllowSorting="True" 
          AllowPaging="True" PageSize="10" onrowdatabound="GridView1_RowDataBound" 
          ondatabound="GridView1_DataBound" EmptyDataText="NO"> 

     <EmptyDataTemplate>No Data</EmptyDataTemplate> 
         </asp:GridView> 

.aspx.cs

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     { 
      e.Row.Cells[0].Text = "Complete"; 
     } 
    } 

    protected void GridView1_DataBound(object sender, EventArgs e) 
    { 
     for (int i = 0; i < GridView1.Rows.Count; i++) 
     { 
      GridViewRow row = GridView1.Rows[i]; 
      var str = GridView1.DataKeys[row.RowIndex]["SomeColumnName"]; 
     } 
+0

如果你是一個初學者,我建議你瞭解MVC,而不是Web表單 –

+0

我添加了一個簡單的例子 – AsifAli72090

回答

0

快速示例:

的.aspx代碼:

<asp:GridView ID="GridView1" runat="server"></asp:GridView> 

層的.cs代碼:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     // Create a datatable as a DataSource of your GridView 
     DataTable dt = new DataTable(); 

     // Add columns 
     if (dt.Columns.Count == 0) 
     { 
      for (int i = 1; i <= 4; i++) 
      { 
       dt.Columns.Add("Column" + i); 
      } 
     } 

     // Add rows 
     for (int i = 1; i <= 10; i++) 
     { 
      dt.Rows.Add("Data" + i, "Data" + i, "Data" + i, "Data" + i); 
     } 

     // Bind datatable to gridview 
     GridView1.DataSource = dt; 
     GridView1.DataBind(); 
    } 
} 

輸出:

enter image description here

相關問題