2015-10-20 64 views
0

下面是我的DataGrid: -動態添加綁定列的數據網格後無法訪問後回

<asp:DataGrid ID="dgPlayers" runat="server" Width="100%" AutoGenerateColumns="False" AlternatingItemStyle-BorderWidth="1px" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" 
      CellPadding="3" ForeColor="Black" GridLines="Vertical" OnItemCommand="dgPlayers_ItemCommand" EnableViewState="true"> 
      <FooterStyle BackColor="#CCCCCC" /> 
      <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" /> 
      <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" /> 
      <SelectedItemStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /> 
      <Columns> 
       <asp:TemplateColumn ItemStyle-CssClass="Edit"> 
        <ItemTemplate> 
         <asp:Button ID="bEdit" Text="Edit" runat="server" CommandName="UPDATE_RECORD"></asp:Button> 
        </ItemTemplate> 
       </asp:TemplateColumn> 
      </Columns> 
     </asp:DataGrid> 

現在搜索按鈕點擊我添加綁定列到它。 代碼添加綁定列如下: -

private void AddColumnsToDataGrid(DataGrid dgPlayers, DataTable dataSource) 
{ 
    try 
    { 
     List<BoundColumn> colList = new List<BoundColumn>();   
     string[] colNamesArr = new string[] { "player_id", "full_name","team_name", "role_name", "position_name", "jersey_number" }; 
     for (int i = 0; i < dataSource.Columns.Count; i++) 
     { 
      if (i < colNamesArr.Length) 
      { 
       BoundColumn column = new BoundColumn(); 
       column.DataField = colNamesArr[i]; 
       //colList.Add(column); 
       if (i == 0) 
       { 
        column.Visible = false; 
       } 

       dgPlayers.Columns.Add(column);      

      } 
      else 
      { 
       break; 
      } 
     } 

    } 
    catch (Exception) 
    { 
     throw; 
    } 
} 

現在我面臨的問題,而我點擊行之一的DataGrid的編輯按鈕,它綁定我的網頁上排數據到其他控制,但我不能獲得動態綁定DataGrid中ItemCommand綁定列....

protected void dgPlayers_ItemCommand(object source, DataGridCommandEventArgs e) 
{ 
    switch (e.CommandName.Trim().ToUpper()) 
     { 
      case "UPDATE_RECORD": 

       this.GetControl<TextBox>("tbFullName").Text = e.Item.Cells[(int)Column.FullName].Text.Replace("&nbsp;", ""); 
       this.GetControl<TextBox>("tbFName").Text = e.Item.Cells[(int)Column.FirstName].Text.Replace("&nbsp;", "");      
       this.GetControl<TextBox>("tbJerseyNum").Text= e.Item.Cells[(int)Column.JerseyNo].Text.Replace("&nbsp;", ""); 
       this.GetControl<TextBox>("tbShirtName").Text = e.Item.Cells[(int)Column.ShirtName].Text.Replace("&nbsp;", ""); 
       this.GetControl<TextBox>("tbLastName").Text = e.Item.Cells[(int)Column.LastName].Text.Replace("&nbsp;", ""); 
       this.GetControl<TextBox>("tbShortName").Text = e.Item.Cells[(int)Column.ShortName].Text.Replace("&nbsp;", ""); 
     } 
} 

任何幫助這個...

回答

0

您需要在後背上重新列。嘗試在加載事件的後續回發期間調用AddColumnsToDatagrid。