我正在尝试找到网格的最后一行,并以编程方式将下拉列表添加到网格中。我的代码编译时没有任何问题,但是在单步执行导致列表永不添加时,我的RowDataBound
永远不会被击中。我有什么不正确的编码?
修改
页面加载,您从下拉列表中选择一个选项。此时调用RefreshdatagridTest。
编辑结束
ASP代码
<div class="CenterGrid">
<asp:DataGrid runat="server" ID="datagridTest" AutoGenerateColumns="false"
Width="500px" CssClass="DataGrids" HorizontalAlign="Center" GridLines="Both"
ShowFooter="true" OnSelectedIndexChanged="datagridTest_SelectedIndexChanged"
OnItemCommand="datagridTest_ItemCommand" RowDataBound="datagridTest_RowDataBound" Visible="true">
<ItemStyle CssClass="row" />
<FooterStyle CssClass="DataGridFooters" />
<HeaderStyle CssClass="DataGridHeaders" />
<Columns>
<asp:BoundColumn DataField="Firefly" HeaderText="Name"></asp:BoundColumn>
<asp:ButtonColumn CommandName="Delete" HeaderText="Delete From List" Text="[Remove]"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>
</div>
C#代码
protected void datagridTest_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex == datagridTest.Items.Count - 1)
{
DropDownList ddl = new DropDownList();
e.Row.Cells[0].Controls.Add(ddl);
}
}
protected void datagridTest_ItemCommand(object source, DataGridCommandEventArgs e)
{
LinkButton lnkbtn = ((LinkButton)e.CommandSource);
if (e.CommandName == "Delete")
{
DataGridItem deletecontrol = (DataGridItem)lnkbtn.NamingContainer;
string bomb = deletecontrol.Cells[0].Text;
RemoveFireflyFromList(bomb);
RefreshdatagridTest();
}
}
网格如何获取数据
protected void listselect_SelectedIndexChanged(object sender, EventArgs e)
{
RefreshdatagridTest();
}
private void RefreshdatagridTest()
{
dataSet = Run sql procedure to get results;
this.datagridTest.DataSource = dataSet;
this.datagridTest.DataBind();
}
答案 0 :(得分:0)
它有点不同,因为在我的例子中我在运行时生成新行但基本上它是如何工作的; 在我的情况下不得不添加一些大小的参数或控件被隐藏。 所以我只需设置控制行并将行添加到网格中。
Mycontrol m = new Mycontrol();
RowDefinition rowDef1 = new RowDefinition();
rowDef1.MaxHeight = m.ExpandedSise;
rowDef1.MinHeight = m.Height;
rowDef1.Height = new GridLength(m.RowDefColapsedSize);
int rownumber = grid2.RowDefinitions.Count;
grid2.RowDefinitions.Insert(rownumber, rowDef1);
Grid.SetRow(m, rownumber);
m.counter.Content = Grid.GetRow(m).ToString();
grid2.Children.Add(m);