简体   繁体   English

如何将按钮添加到绑定到Datatable ASP.Net的GridView

[英]How to add button to GridView bound to Datatable ASP.Net

I have a GridView in in my webform. 我的网络表单中有一个GridView。 I set the data source as a DataTable, and bind it. 我将数据源设置为DataTable,并将其绑定。 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 我的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. 并将onrowcommand事件添加到gridview,还将datakeyName属性添加到gridview。

 <asp:GridView  ID="GridView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="Id"
            AutoGenerateColumns="false" onrowcommand="GridView1_RowCommand">

then write code on 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+"'";
            }
        }

Use the CommandArgument to pass in any additional value (first databound field value) to the CommandName which you can manipulate in the code behind. 使用CommandArgument将任何附加值(第一个数据绑定字段值)传递给CommandName ,您可以在后面的代码中对其进行操作。

Change your TemplateField as below 如下更改您的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>

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 . 或者只是将RowIndex作为CommandArgument传入,然后可以使用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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM