[英]ASP.NET gridview - how to add dynamically populated dropdown to dynamically bound gridview
[英]How to add button to GridView bound to Datatable ASP.Net
我的网络表单中有一个GridView。 我将数据源设置为DataTable,并将其绑定。 当我尝试通过选择“启用编辑/更新/删除”来添加编辑按钮时,该按钮没有显示。
但是,我设法手动显示了该按钮。 单击按钮时,如何获得单击按钮的行的第一个单元格值?
我的GridView
<asp:GridView ID="SOGridView" runat="server" ShowHeader="False"
onrowdatabound="SOGridView_RowDataBound" onrowcommand="SOGridView_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Actions">
<ItemTemplate>
<asp:Button ID="btnAddNewSO" runat="server" CommandName="Select" height="40px"
Text="Add" Width="75px" onclick="btnAddNewSO_Click" />
</ItemTemplate>
</asp:TemplateField></Columns>
</asp:GridView>
在按钮上单击
SOGridView.DataSource = dt;
SOGridView.DataBind();
我的数据表
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Col1", typeof(string)));
dt.Columns.Add(new DataColumn("Col2", typeof(string)));
dt.Columns.Add(new DataColumn("Col3", typeof(string)));
dt.Columns.Add(new DataColumn("Col4", typeof(string)));
dt.Columns.Add(new DataColumn("Col5", typeof(string)));
dt.Columns.Add(new DataColumn("Col6", typeof(string)));
dt.Columns.Add(new DataColumn("Col7", typeof(string)));
在按钮上单击页面,
DataRow dr = dt.NewRow();
dr["Col1"] = ddlitemcategory.SelectedValue;
dr["Col2"] = ddlitems.SelectedValue;
dr["Col3"] = textQty.Text;
dr["Col4"] = textDisc.Text;
dr["Col5"] = textAmount.Text;
dr["Col6"] = textPDeliveryDate.Text;
dr["Col7"] = textPShipmentDate.Text;
dt.Rows.Add(dr);
SOGridView.DataSource = dt;
SOGridView.DataBind();
您只需要为按钮重写代码,如下所示
<asp:Button ID="addbtn" runat="server" Text="save" CommandName="SAVE" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"/>
并将onrowcommand
事件添加到gridview,还将datakeyName属性添加到gridview。
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="Id"
AutoGenerateColumns="false" onrowcommand="GridView1_RowCommand">
然后在rowcommand上编写代码
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "SAVE")
{
int index = Convert.ToInt32(e.CommandArgument.ToString());
int id=Convert.ToInt32(GridView1.DataKeys[index].Value.ToString());
string deltequer="delete from yourtablename where id='"+id+"'";
}
}
使用CommandArgument
将任何附加值(第一个数据绑定字段值)传递给CommandName
,您可以在后面的代码中对其进行操作。
如下更改您的TemplateField
<asp:TemplateField HeaderText="Actions">
<ItemTemplate>
<asp:Button ID="btnAddNewSO" runat="server" CommandName="Select" height="40px"
Text="Add" Width="75px" CommandArgument='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
然后在您的代码后面。
protected void SOGridView_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
// Retrieve the CommandArgument property
int cellvalue = Convert.ToInt32(e.CommandArgument); // or convert to other datatype
}
}
或者只是将RowIndex
作为CommandArgument
传入,然后可以使用FindControl
访问相应行的任何控件。 所以现在您的按钮定义将变为
<asp:Button ID="btnAddNewSO" runat="server" CommandName="Select" height="40px"
Text="Add" Width="75px" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
还有你的代码
if (e.CommandName == "Select")
{
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button
// from the Rows collection.
GridViewRow row = SOGridView.Rows[index];
// use FindControl to find any control you want to access from the row collection
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.