I have a GridView in in my webform. I set the data source as a DataTable, and bind it. When I tried adding an edit button by selecting "Enable edit/update/delete", it wasn't showing up.
But, I managed to show the button manually. On button click, how do I get the first cell value of the row, of which the button is clicked ?
My 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>
On Button Click,
SOGridView.DataSource = dt;
SOGridView.DataBind();
MY DATA TABLE
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)));
On Button Click in the page,
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();
you just need rewrite code for button as follow
<asp:Button ID="addbtn" runat="server" Text="save" CommandName="SAVE" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"/>
and add onrowcommand
event to gridview and also add datakeyName attribute to gridview.
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="Id"
AutoGenerateColumns="false" onrowcommand="GridView1_RowCommand">
then write code on 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+"'";
}
}
Use the CommandArgument
to pass in any additional value (first databound field value) to the CommandName
which you can manipulate in the code behind.
Change your TemplateField
as below
<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>
and then in your code behind.
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
}
}
Or just pass in the RowIndex
as the CommandArgument
and then you can access any control of the respective row using FindControl
. So now your button definition would become
<asp:Button ID="btnAddNewSO" runat="server" CommandName="Select" height="40px"
Text="Add" Width="75px" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
and your code behind
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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.