简体   繁体   English

以编程方式将按钮添加到GridView

[英]Add a Button to a GridView programmatically

I have a GridView and I am adding BoundField s in it using code. 我有一个GridView并且正在使用代码在其中添加BoundField Now I want to add buttons for edit and delete using code. 现在,我想添加按钮以使用代码进行编辑和删除。 I know how to add a ButtonField to the grid using code, but I want to add a button, as ButtonField does not have the CommandArgument property. 我知道如何使用代码将ButtonField添加到网格中,但是我想添加一个按钮,因为ButtonField没有CommandArgument属性。

Here is my GridView markup: 这是我的GridView标记:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellPadding="4"
                BorderWidth="1px" ForeColor="#333333" GridLines="None">
     <AlternatingRowStyle BackColor="White" />
     <FooterStyle BackColor="#990000" ForeColor="White" Font-Bold="True" />
     <HeaderStyle Height="30px" BackColor="#990000" Font-Bold="True" ForeColor="White" />
     <PagerStyle ForeColor="#333333" HorizontalAlign="Center" BackColor="#E2E2E2" />
     <RowStyle CssClass="test" BackColor="#E2E2E2" Height="25px" BorderWidth="1px" ForeColor="#333333" />
     <SelectedRowStyle BackColor="#E2E2E2" Font-Bold="True" ForeColor="Navy" />
     <SortedAscendingCellStyle BackColor="#FDF5AC" />
     <SortedAscendingHeaderStyle BackColor="#4D0000" />
     <SortedDescendingCellStyle BackColor="#FCF6C0" />
     <SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>

And here is my C# code: 这是我的C#代码:

 GridView1.DataKeyNames = new string[] { PrimaryKey };

 if (dtOutPutResult.Rows.Count > 0)
 {
     foreach (DataColumn dcDtOutPutResult in dtOutPutResult.Columns)
     {
         foreach (DataRow drDtColumns in dtColumns.Rows)
         {
             if (drDtColumns["OrignalColumn"].ToString() == dcDtOutPutResult.ColumnName)
             {
                 BoundField bfield = new BoundField();
                 bfield.DataField = dcDtOutPutResult.ColumnName;
                 bfield.HeaderText = drDtColumns["DisplayColumn"].ToString();
                 GridView1.Columns.Add(bfield);
             }
         }
     }

     foreach (DataRow dr in dtOutPutResult.Rows)
     {
         var buttonField = new ButtonField
         {
             ButtonType = ButtonType.Button,
             Text = "My button",
             CommandName = "DoSomething",
         };
         GridView1.Columns.Add(buttonField);
         break;
     }

     GridView1.DataSource = dtOutPutResult;
     GridView1.DataBind();
     lblMessage.Visible = false;
 }
 else
 {
     GridView1.DataSource = dtOutPutResult;
     GridView1.DataBind();
     lblMessage.Visible = true;
     lblMessage.Style.Add("Color", "Red");
     lblMessage.Text = "No Record Found.";
 }

It sounds like you want to add a CommandField . 听起来您想添加CommandField

CommandField cField = new CommandField();
cField.EditText = "Edit";
cField.DeleteText = "Delete";
cField.UpdateText = "Update";
cField.CancelText = "Cancel";

cField.ShowEditButton = true;
cField.ShowDeleteButton = true;

GridView1.Columns.Add(cField);

These buttons will send the CommandArgument like you want, and should trigger the RowCommand event (if you want to handle that). 这些按钮将根据需要发送CommandArgument,并应触发RowCommand事件(如果要处理)。

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

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