2

私はを持っており、コードを使用してそれにsをGridView追加BoundFieldしています。次に、コードを使用して編集および削除するためのボタンを追加します。コードを使用してグリッドにを追加する方法は知っていますが、CommandArgumentプロパティがないためButtonField、ボタンを追加したいと思います。ButtonField

これが私の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>

そしてこれが私の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.";
 }
4

1 に答える 1

1

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

これらのボタンは、必要に応じてCommandArgumentを送信し、RowCommandイベントをトリガーする必要があります(これを処理する場合)。

于 2013-03-15T14:37:11.670 に答える