Click here to Skip to main content
15,915,019 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
How to read the Gridview dropdown values in the gridview_rowcommand.
I want to update the values based on the dropdown getting values in the gridview_rowcommand.
How to do that?
See my code

<asp:TemplateField HeaderText="From Period" HeaderStyle-HorizontalAlign="Left"> 
                                    <edititemtemplate> 
                                        <asp:DropDownList ID="cmbFrm" runat="server" Style="width: 150px;" 
                                            DataSourceID="dsOtherDept1" DataTextField="FromPeriod" DataValueField="FromPeriod"
                                            AppendDataBoundItems="true">
                                            <asp:ListItem Text="Please Select" Value="">
                                        
                                    </edititemtemplate> 
                                    <itemtemplate> 
                                        <asp:Label ID="lblFrm" runat="server" Text='<%# Eval("FromPeriod") %>'> 
                                    </itemtemplate> 
                                     <HeaderStyle HorizontalAlign="Left" />
                                  
                                 <asp:TemplateField HeaderText="To Period" HeaderStyle-HorizontalAlign="Left"> 
                                    <edititemtemplate> 
                                        <asp:DropDownList ID="cmbTo" runat="server" Style="width: 150px;" 
                                            DataSourceID="dsOtherDept1" DataTextField="ToPeriod" DataValueField="ToPeriod"
                                            AppendDataBoundItems="true">
                                            <asp:ListItem Text="Please Select" Value="">
                                        
                                    </edititemtemplate> 
                                    <itemtemplate> 
                                        <asp:Label ID="lblTo" runat="server" Text='<%# Eval("ToPeriod") %>'> 
                                    </itemtemplate> 
                                     <HeaderStyle HorizontalAlign="Left" />
                                  
                                <asp:TemplateField HeaderText="Edit" ShowHeader="False" HeaderStyle-HorizontalAlign="Left"> 
                                    <edititemtemplate> 
                                        <asp:LinkButton ID="lbkUpdate" runat="server" CausesValidation="True" CommandName="Update"  CommandArgument='<%#Eval("emp_number") %>' Text="Update"> 
                                        <asp:LinkButton ID="lnkCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"> 
                                    </edititemtemplate> 
                                    <itemtemplate> 
                                        <asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="False" CommandName="Edit"  CommandArgument='<%#Eval("emp_number") %>' Text="Edit"> 
                                    </itemtemplate> 
                                    <HeaderStyle HorizontalAlign="Left" />
                                 
                                <asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" /> 
                             
                    
                    <asp:SqlDataSource ID="dsOtherDept1" runat="server"   
                            ConnectionString="<%$ConnectionStrings:MPP%>"
                            SelectCommand="Select distinct V.emp_number as emp_number,emp_name+' '+emp_initial as name,emp_designation,Manpower_Division_ID,FromPeriod, ToPeriod from idpeapp.dbo.view_employee V inner join OtherDeptEmp O on V.emp_number = O.emp_number order by name">




ASPX.VB Code
------------
Protected Sub grdDeptEmp_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdDeptEmp.RowCommand
        'Dim list As DropDownList = TryCast(e.Row.FindControl("cmbFrm"), DropDownList)
        'cmbFrm.SelectedIndex = cmbFrm.Items.IndexOf(cmbFrm.Items.FindByValue(DataBinder.Eval(e.Row.DataItem, "FromPeriod")))
        Dim emp_number As Integer = Convert.ToInt32(e.CommandArgument)

        If e.CommandName = "update" Then
            'Dim list As DropDownList = TryCast(e.Row.FindControl("cmbFrm"), DropDownList)
            'get the EmpNo of the clicked row
            dsOtherDept1.UpdateCommand  = "update OtherDeptEmp set FromPeriod='" &  & "',ToPeriod='" & & "' where emp_number= '" & EmpNo & "'"
            dsOtherDept1.DataBind()
        End If
    End Sub
Posted
Updated 11-Jul-11 19:01pm
v2

1 solution

You can use FindControl in GridView RowCommand event.

Use the following code in GridView RowCommand event.

VB
Dim lbtn As System.Web.UI.WebControls.LinkButton = TryCast(e.CommandSource, System.Web.UI.WebControls.LinkButton)
    If lbtn IsNot Nothing Then
        Dim row As GridViewRow = TryCast(lbtn.NamingContainer, GridViewRow)
        If row IsNot Nothing Then
            Dim cmbFrm As DropDownList = TryCast(row.FindControl("cmbFrm"), DropDownList)
            string fromperiod =cmbFrm.SelectedItem.Value
            'similarly find the control cmbTo and update
        End If
    End If
 
Share this answer
 
v2
Comments
Venkatesh Mookkan 13-Jul-11 23:48pm    
Good call

Note:
FindControl method is applicable for TemplateColumn field.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900