2016-05-12 105 views
0

我有GridViewcheckbox,dropdownbutton作爲templatefields。 點擊按鈕,我必須更新數據庫中的整個行。更新GridViewRow按鈕點擊該行

這是我aspx

<asp:GridView runat="server" ID="Gdv" AutoGenerateColumns="False" Font-Size="Small" CssClass="grid" BackColor="White" BorderWidth="0px" CellPadding="4" Width="100%" AllowSorting="True" SkinID="GVSalesManager" GridLines="none" AllowPaging="true" PageSize="10" PagerStyle-ForeColor="#0066cc" PagerStyle-CssClass="gvPagerCss" PagerStyle-Font-Underline="true" PagerStyle-Wrap="true" 
OnPageIndexChanging="GdvCPRetailerMap_PageIndexChanging" OnRowDataBound="GdvCPRetailerMap_RowDataBound"> 
<Columns> 
    <asp:BoundField ReadOnly="true" HeaderText="S.No" DataField="S.No." SortExpression="SNo"> 
    <ItemStyle HorizontalAlign="Center" Width="2%" /> 
    <HeaderStyle HorizontalAlign="Center" Font-Bold="true" Width="2%"/> 
    </asp:BoundField> 

    <asp:TemplateField ItemStyle-Width="3%" ItemStyle-HorizontalAlign="Center"> 
     <ItemTemplate> 
     <asp:CheckBox ID="chkRow" runat="server" OnCheckedChanged="chkRow_CheckedChanged" AutoPostBack="true"/> 
     </ItemTemplate> 
    </asp:TemplateField> 

    <asp:BoundField ReadOnly="true" HeaderText="Customer Id" ItemStyle-HorizontalAlign="Center" DataField="CustomerID" SortExpression="CustomerID"> 
<ItemStyle HorizontalAlign="Center" Width="10%" /> 
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Width="10%"/> 
    </asp:BoundField> 

<asp:BoundField ReadOnly="true" HeaderText="Firm's Name" ItemStyle-HorizontalAlign="Center" DataField="Firm's Name" SortExpression="SNo"> 
<ItemStyle HorizontalAlign="Center" Width="20%" /> 
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Width="20%"/> 
    </asp:BoundField> 

<asp:BoundField ReadOnly="true" HeaderText="Retailer Name" ItemStyle-HorizontalAlign="Center" DataField="Retialer Name" SortExpression="SNo"> 
    <ItemStyle HorizontalAlign="Center" Width="20%" /> 
    <HeaderStyle HorizontalAlign="Center" Font-Bold="true" Width="20%"/> 
</asp:BoundField> 

<asp:BoundField ReadOnly="true" HeaderText="Pesticide Licence No." ItemStyle-HorizontalAlign="Center" DataField="Pesticide Licence No" SortExpression="SNo"> 
<ItemStyle HorizontalAlign="Center" Width="10%" /> 
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Width="10%"/> 
</asp:BoundField> 

<asp:TemplateField HeaderText="Channel Partner-1" HeaderStyle-Font-Bold="true" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center"> 
<ItemTemplate> 
<asp:DropDownList ID="ddlCP1" Enabled="false" ToolTip="Please Click on the checkbox to change the prefered CP" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCP1_SelectedIndexChanged"></asp:DropDownList> 
</ItemTemplate> 
</asp:TemplateField> 

<asp:TemplateField HeaderText="Channel Partner-2" HeaderStyle-Font-Bold="true" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center"> 
<ItemTemplate> 
<asp:DropDownList ID="ddlCP2" Enabled="false" ToolTip="Please Click on the checkbox to change the prefered CP" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCP2_SelectedIndexChanged"></asp:DropDownList> 
</ItemTemplate> 
</asp:TemplateField> 

<asp:TemplateField ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center"> 
<ItemTemplate> 
<asp:Button runat="server" ID="BtnUpdateRow" Text="Update" ToolTip="Update This Record?" OnClick="BtnUpdateRow_Click" BackColor="#336699" ForeColor="#ffffff"/> 
</ItemTemplate> 
</asp:TemplateField> 

    </Columns> 
</asp:GridView> 

這是updatebutton事件

protected void BtnUpdateRow_Click(object sender, EventArgs e) 
{ 
    Button btn = (Button)sender; 
    GridViewRow grow = (GridViewRow)btn.NamingContainer; 
    //btn. 
    CheckBox chkbx = (CheckBox)grow.FindControl("chkRow"); 

    if (chkbx.Checked != true) 
    { 
     labelErrormessage.Visible = true; 
     labelErrormessage.ForeColor = System.Drawing.Color.Red; 
     labelErrormessage.Text = "Please Check/Uncheck the CheckBox and Click Update"; 
     return; 

    } 
    else 
    { 
     /*--Code to update the row's record in the edatabase.--*/ 
     ?? 

     labelErrormessage.Visible = true; 
     labelErrormessage.ForeColor = System.Drawing.Color.Green; 
     labelErrormessage.Text = "Updated Succesfully"; 
     return; 
    } 
} 

我如何在數據庫中讀出其中的按鈕被點擊的行和更新其dropdownlist選擇的值?

回答

0

如果你在gridview中顯示了某種標識數據,你可以從這個行的單元格中獲得這個值,例如grow.Cells[0].Text,然後在數據庫中找到記錄並更新它的值。

如果這是您不想顯示給用戶的信息,您也可以隱藏該id。該行仍將位於索引0處但隱藏。

樣品:

else 
    { 
     var record = queryDatabase(grow.Cells[0].Text); 

     //update record values 

     updateRecord(record); 

     labelErrormessage.Visible = true; 
     labelErrormessage.ForeColor = System.Drawing.Color.Green; 
     labelErrormessage.Text = "Updated Succesfully"; 
     return; 
    }