下面是我的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(" ", "");
this.GetControl<TextBox>("tbFName").Text = e.Item.Cells[(int)Column.FirstName].Text.Replace(" ", "");
this.GetControl<TextBox>("tbJerseyNum").Text= e.Item.Cells[(int)Column.JerseyNo].Text.Replace(" ", "");
this.GetControl<TextBox>("tbShirtName").Text = e.Item.Cells[(int)Column.ShirtName].Text.Replace(" ", "");
this.GetControl<TextBox>("tbLastName").Text = e.Item.Cells[(int)Column.LastName].Text.Replace(" ", "");
this.GetControl<TextBox>("tbShortName").Text = e.Item.Cells[(int)Column.ShortName].Text.Replace(" ", "");
}
}
对此有任何帮助......
答案 0 :(得分:0)
您需要在回发期间重新创建列。尝试在load事件中的后续回发期间调用AddColumnsToDatagrid。