2014-07-02 76 views
0

我想在我的DataGrid對象中綁定列。代碼工作正常之前,我添加它們,但現在我得到重複的列,特別是我不想要的一列。這裏是ASP:ASP:DataGrid顯示兩列的列

 <ASP:DataGrid id="UserDataGrid" AutoGenerateColums="False" runat="server"> 
      <Columns> 
       <asp:EditCommandColumn CancelText="Cancel" EditText="Edit" UpdateText="Update" HeaderText="Edit item"> 
        <ItemStyle Wrap="False" /> 
        <HeaderStyle Wrap="False"/> 
       </asp:EditCommandColumn> 
       <asp:BoundColumn DataField="Email" HeaderText="Email" ReadOnly="false" SortExpression="Email" /> 
       <asp:BoundColumn DataField="UserID" HeaderText="User ID" ReadOnly="false" SortExpression="UserID" /> 
       <asp:BoundColumn DataField="FullName" HeaderText="User Name" ReadOnly="false" SortExpression="FullName" /> 
      </Columns> 
     </ASP:DataGrid> 

而後面的代碼:

DataTable dtUsers = new DataTable(); 
    dtUsers = dataAccessManager.ExecuteSQLForTable("SELECT * FROM tblUser"); 
    UserDataGrid.DataSource = dtUsers; 
    UserDataGrid.DataBind(); 

一切我讀過說設置AutoGenerateColumns爲false應該解決的問題,但它沒有做任何事情。

回答

1

看着你提供的代碼,AutoGenerateColumns似乎拼寫錯誤。如果你解決這個問題,它應該工作得很好

<asp:DataGrid ID="UserDataGrid" AutoGenerateColumns="false" runat="server"> 
+1

這麼多facepalms。我認爲VS2010會抱怨與課程不匹配的屬性? –

0

解決方法:1

Nomally的的AutoGenerateColumns屬性爲true,如果我們一個DataGrid綁定到一個表,在DataGrid將顯示該表的所有列。只有當您將自動生成列設置爲false時,纔可以讓DataGrid顯示您分配的列。

但根據你這不是working.Ok

解決方案:2

可以隱藏DataGrid列

DataTable dtUsers = new DataTable(); 
    dtUsers = dataAccessManager.ExecuteSQLForTable("SELECT * FROM tblUser"); 
    UserDataGrid.DataSource = dtUsers; 
    UserDataGrid.DataBind(); 
    //Right Below line for hide columns 
    UserDataGrid.Columns[0].Visible = false;//Hide First column of the DataGrid 
    UserDataGrid.Columns[1].Visible = false;//Hide Second column of the DataGrid 
    UserDataGrid.Columns[2].Visible = false;//Hide Third column of the DataGrid 

解決方案:3

您可以刪除DataGrid列

DataTable dtUsers = new DataTable(); 
    dtUsers = dataAccessManager.ExecuteSQLForTable("SELECT * FROM tblUser"); 
    UserDataGrid.DataSource = dtUsers; 
    UserDataGrid.DataBind(); 
    //Right Below line for Remove columns 

    UserDataGrid.Columns[0].Remove(); //Remove First column of the DataGrid 
    UserDataGrid.Columns[1].Remove(); //Remove Second column of the DataGrid 
    UserDataGrid.Columns[2].Remove(); //Remove Third column of the DataGrid 

最好的祝願