2016-05-12 7 views
0

GridViewcheckboxdropdownおよびbuttonはテンプレートフィールドです。 ボタンをクリックすると、データベースの行全体を更新する必要があります。その行のボタンクリックで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> 

これは私がボタンをクリックされた行を読み込み、データベース内のdropdownlist選択された値を更新するにはどうすればよい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; 
    } 
} 

です?

答えて

0

グリッドビューに何らかの識別データが表示されている場合は、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; 
    } 
関連する問題