【问题标题】:How to add button to GridView bound to Datatable ASP.Net如何将按钮添加到绑定到 Datatable ASP.Net 的 GridView
【发布时间】:2014-09-12 18:27:21
【问题描述】:

我的网络表单中有一个 GridView。我将数据源设置为DataTable,并绑定它。当我尝试通过选择“启用编辑/更新/删除”来添加编辑按钮时,它没有显示出来。

但是,我设法手动显示了按钮。单击按钮时,如何获取单击该按钮的行的第一个单元格值?

我的网格视图

<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();

【问题讨论】:

  • cam 你展示你的绑定gridview的代码
  • 您是否使用BoundColumn&lt;asp:TemplateField 进行列定义?
  • 我正在使用

标签: asp.net gridview


【解决方案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+"'";
            }
        }

【讨论】:

  • 这很好用,我得到了 GridView 的 Row 索引值。正如我所提到的,我正在从数据表中填充网格视图。如何知道要编辑的行是否与数据表中的行相同?
  • 我没听懂你。告诉我你想对按钮的点击事件执行什么操作。你想更新数据库中的记录吗?
  • 其实我想删除记录。我的 gridview 绑定到数据表。单击按钮 btnAddNewSO,应删除 GridView 以及数据表中的行。抱歉命名约定。我应该把它改成“DeletSO”。
  • 是否可以从数据表中删除,而不是使用 SQL ?
  • “不使用 sql”。从哪里填充您的数据表?如果你不想写 sql 查询,那么还有太多其他的东西,比如 EF 或 ORM。
【解决方案2】:

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

【讨论】:

  • 但是,我们从哪里得到 Eval("ID") 的值?它像第一个单元格标题吗?
  • 不,这是数据源中的字段之一。
  • 我如何使用这个按钮来编辑数据表中的项目?
  • 您必须从数据表中删除相应的行(匹配键),并使用更新的数据源重新绑定网格,例如 SOGridView.DataSource = dt; SOGridView.DataBind();
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-11
  • 1970-01-01
  • 2010-10-30
  • 2014-12-22
相关资源
最近更新 更多