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

时间:2014-07-22 04:17:29

标签: asp.net gridview

我的网络表单中有一个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();

2 个答案:

答案 0 :(得分:1)

你只需按下按钮重写代码

<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+"'";
            }
        }

答案 1 :(得分:0)

使用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
}