-2
 GridView gv = new GridView();

BoundField farmername = new BoundField();
farmername.HeaderText = "Farmer Name";
farmername.DataField = "farmername";
gv.Columns.Add(farmername);


BoundField villagename = new BoundField();
villagename.HeaderText = "Village Name";
villagename.DataField = "village";
gv.Columns.Add(villagename);

BoundField feedtype = new BoundField();
feedtype.HeaderText = "Feed Type";
feedtype.DataField = "feedtype";
gv.Columns.Add(feedtype);


BoundField bf50kg = new BoundField();
bf50kg.HeaderText = "50 Kg Bags";
bf50kg.DataField = "noof50kgsbags";
gv.Columns.Add(bf50kg);

CommandField cf = new CommandField();
cf.ButtonType = ButtonType.Button;
cf.ShowCancelButton = true;
cf.ShowEditButton = true;
gv.Columns.Add(cf);

gv.RowEditing += new GridViewEditEventHandler(gv_RowEditing);
gv.RowUpdating += new GridViewUpdateEventHandler(gv_RowUpdating);
gv.RowCancelingEdit += new GridViewCancelEditEventHandler(gv_RowCancelingEdit);

gv.AutoGenerateColumns = false;
gv.ShowFooter = true;
gv.DataSource = dtIndentDetails;
gv.DataBind();

当我单击编辑按钮时,它不会拆分为更新、取消按钮。如何使用命令字段执行此操作。如果我在 aspx 页面中添加 gridview,它的拆分以更新和取消

在此处输入图像描述

4

3 回答 3

0

i suggest you to work out in this way. i have made an example.

will display you the edit command button do it from back end.

This is my grid:

<asp:GridView runat="server" AutoGenerateColumns="false" ID="grdCustomers" DataKeyNames="CustomerID" AllowSorting="true"
                     OnRowEditing="grdCustomers_RowEditing" OnRowDeleting="grdCustomers_RowDeleting" HeaderStyle-BackColor = "green"
                     OnRowUpdating="grdCustomers_RowUpdating" OnRowCancelingEdit="grdCustomers_RowCancelingEdit"  AllowPaging="false"  ShowFooter="true" >
                    <Columns>
                        <asp:TemplateField HeaderText="Customer Id">
                            <ItemTemplate>
                                <asp:Label Text='<%#Eval("CustomerID")%>' HeaderText=""  runat="server" ID="lblcustomerId"   />
                            </ItemTemplate>

                        </asp:TemplateField>


                             <asp:TemplateField HeaderText="Contact Name">
                            <ItemTemplate>
                                 <asp:Label Text='<%#Eval("ContactName")%>' ID="lblCustomerName" runat="server" />

                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtContactName" runat="server" />
                            </EditItemTemplate>

                        </asp:TemplateField>
                           <asp:TemplateField HeaderText="City">
                            <ItemTemplate>

                                <asp:Label id="lblCity" Text='<%#Eval("City")%>' runat="server" /> 
                            </ItemTemplate>
                            <EditItemTemplate>
                              <asp:DropDownList ID="ddlCity1" runat="server" DataTextField="City" DataValueField="City"></asp:DropDownList>
                            </EditItemTemplate>

                        </asp:TemplateField>
                       <asp:CommandField ShowEditButton="true" HeaderText="Edit"  />


                    </Columns>

                </asp:GridView>

My Back End Code to bind data:

Protected Sub GetAllCustomersData()
    ' List to Hold the results

    'Type of binding
    Dim myBinding As New BasicHttpBinding

    'Endpoint name defining
    Dim myEndPoint As New EndpointAddress(ConfigurationManager.AppSettings("CustomerServiceUrl").ToString())

    'Registering Service Reference
    Dim MychannelFactory As ChannelFactory(Of ICustomer) = New ChannelFactory(Of ICustomer)(myBinding, myEndPoint)

    'Creating a Channel to access Services
    Dim client As ICustomer = MychannelFactory.CreateChannel

    lst = client.GetCustomerData()
    grdCustomers.DataSource = lst
    grdCustomers.DataBind()
End Sub

For Row Editing:

Protected Sub grdCustomers_RowEditing(sender As Object, e As GridViewEditEventArgs) Handles grdCustomers.RowEditing
    grdCustomers.EditIndex = e.NewEditIndex
    GetAllCustomersData()

End Sub

For Row Updating:

Protected Sub grdCustomers_RowUpdating(sender As Object, e As GridViewUpdateEventArgs) Dim result As Boolean Dim CustObj As New Services.Entities.Customers.Customer CustObj.CustomerID = DirectCast(grdCustomers.Rows(e.RowIndex).FindControl("lblCustomerId"), Label).Text CustObj.ContactName = DirectCast(grdCustomers.Rows(e.RowIndex).FindControl("txtContactName"), TextBox).Text CustObj.City = DirectCast(grdCustomers.Rows(e.RowIndex).FindControl("ddlCity1"), DropDownList).SelectedValue.ToString

    'Type of binding
    Dim myBinding As New BasicHttpBinding

    'Endpoint name defining
    Dim myEndPoint As New EndpointAddress(ConfigurationManager.AppSettings("CustomerServiceUrl").ToString())

    Dim myChannelFactory As ChannelFactory(Of ICustomer) = New ChannelFactory(Of ICustomer)(myBinding, myEndPoint)
    'Creating a Channel to access Services
    Dim client As ICustomer = myChannelFactory.CreateChannel
    result = client.UpdateCustomerDetails(CustObj)
    grdCustomers.EditIndex = -1
    GetAllCustomersData()
End Sub

For Row Edit Cancel Button:

  Protected Sub grdCustomers_RowCancelingEdit(sender As Object, e As GridViewCancelEditEventArgs)
    grdCustomers.EditIndex = -1
    GetAllCustomersData()
  End Sub
于 2013-10-16T12:41:20.440 回答
0

将您的命令按钮替换为链接或图像按钮。放置这一个衬垫。

cf.CommandName = "Edit";

您也可以对取消和更新执行相同的操作,并分别传入命令名称“取消”和“更新”。这将是一种解决方法。我也很难让动态生成的命令按钮触发。

更新

另一种有帮助的干净方法。请参阅此处投递箱上的附件

于 2013-10-14T09:25:01.463 回答
0

分配命令名称并在事件处理程序中使用这样的代码进行编辑和更新。我用过vb.net。

Create: Link Button b= new Link Button()
cf.ShowEditButton = true;
cf.CommandName="Edit";
gv.Columns.Add(cf);

使用项目模板和编辑模板。当编辑命令自动触发时,将执行行编辑方法。

Protected Sub grdCustomers_RowEditing(sender As Object, e As GridViewEditEventArgs)       Handles grdCustomers.RowEditing
grdCustomers.EditIndex = e.NewEditIndex
GetAllCustomersData()
End Sub

对于更新:

Protected Sub grdCustomers_RowUpdating(sender As Object, e As GridViewUpdateEventArgs)
Dim result As Boolean
Dim CustObj As New Services.Entities.Customers.Customer
CustObj.CustomerID = DirectCast(grdCustomers.Rows(e.RowIndex).FindControl("lblCustomerId"), Label).Text
CustObj.ContactName = DirectCast(grdCustomers.Rows(e.RowIndex).FindControl("txtContactName"), TextBox).Text
CustObj.City = DirectCast(grdCustomers.Rows(e.RowIndex).FindControl("ddlCity1"), DropDownList).SelectedValue.ToString

'Type of binding
Dim myBinding As New BasicHttpBinding

'Endpoint name defining
Dim myEndPoint As New   EndpointAddress(ConfigurationManager.AppSettings("CustomerServiceUrl").ToString())

Dim myChannelFactory As ChannelFactory(Of ICustomer) = New ChannelFactory(Of ICustomer)(myBinding, myEndPoint)

'Creating a Channel to access Services
Dim client As ICustomer = myChannelFactory.CreateChannel

result = client.UpdateCustomerDetails(CustObj)

grdCustomers.EditIndex = -1
GetAllCustomersData()
Sub End 

对于取消更新:

Protected Sub grdCustomers_RowCancelingEdit(sender As Object, e As GridViewCancelEditEventArgs)
grdCustomers.EditIndex = -1
GetAllCustomersData()
End Sub
于 2013-10-15T16:56:21.407 回答