动态添加到数据网格的绑定列在发回后不可访问

本文关键字:访问 绑定 添加 数据 数据网 网格 动态 | 更新日期: 2023-09-27 18:18:23

下面是我的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;", "");
        }
}

动态添加到数据网格的绑定列在发回后不可访问

您需要在回发期间重新创建列。尝试在加载事件的后续回发期间调用AddColumnsToDatagrid。