Click here to Skip to main content
15,904,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Can anyone tell me how to get the selected value of a dropdownlist in gridview on the onrowcommand event

Thanks
Posted

1 solution

at 1st you need to set CommandArgument=row index, then use this code in onrowcommand event.

.aspx code:
ASP.NET
<asp:GridView ID="dtgView" runat="server" AutoGenerateColumns="False" CellPadding="4"
                                                    ForeColor="#333333" GridLines="None" ShowFooter="true" OnRowCommand="dtgView_RowCommand">
                                                    <RowStyle BackColor="#EFF3FB" />
                                                    <Columns>
                                                        <asp:TemplateField>
                                                            <ItemTemplate>
                                                                <asp:ImageButton ID="btnSelect" runat="server" CommandName="RSelect" CommandArgument='<%#Eval("RIndex") %>'
                                                                    ImageUrl="~/Icon/page_edit.gif" />
                                                            </ItemTemplate>
                                                        </asp:TemplateField>
                                                        <asp:TemplateField>
                                                            <ItemTemplate>
                                                                <asp:DropDownList ID="ddl" runat="server">
                                                                </asp:DropDownList>
                                                            </ItemTemplate>
                                                        </asp:TemplateField>
                                                    </Columns>
                                                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                                                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                                                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                                                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                                                    <EditRowStyle BackColor="#2461BF" />
                                                    <AlternatingRowStyle BackColor="White" />
                                                </asp:GridView>



.cs code:
C#
class dtgData
        {
            int _RIndex;
            public int RIndex
            {
                get { return _RIndex; }
                set { _RIndex = value; }
            }
        }

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<dtgData> list = new List<dtgData>();
                dtgData ob = new dtgData();
                for (int i = 0; i <= 4; i++)
                {
                    ob = new dtgData();
                    ob.RIndex = i;
                    list.Add(ob);
                }
                dtgView.DataSource = list;
                dtgView.DataBind();
            }
        }

protected void dtgView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //Need to set CommandArgument=row index
            int rowCount = 0;
            foreach (GridViewRow row in dtgView.Rows)
            {
                if (rowCount == Convert.ToInt16(e.CommandArgument))
                {
                    string str = ((DropDownList)row.FindControl("ddl")).SelectedValue.ToString();
                }
                rowCount++;
            }
        }


here,
dtgView=GridView
ddl=DropDownList inside your gridview

try this...
 
Share this answer
 
v2
Comments
Jim Jos 14-Oct-12 20:11pm    
Good answer!! my 5!
csharpbd 15-Oct-12 15:29pm    
thanks 4 review my code...
Member 9291223 14-Oct-12 20:16pm    
Thanks but any hints on how to set the CommandArgument=row index...I tried to use
int indexx = Convert.ToInt32(e.CommandArgument);

GridViewRow rowx = gvunclassified.Rows[indexx];
int rowCount = Convert.ToInt32(rowx.Cells[5].Text);
but this doesnt work
csharpbd 15-Oct-12 15:29pm    
see my solution 4 full example...
Member 9291223 15-Oct-12 16:39pm    
I understood your code above but the problem is in the way that i bind my Datasource.Its actually a stored procedure used in databing and dropdown list is populated with a datasource.The below show my gridview and its binding on page load.
string username = System.Web.HttpContext.Current.User.Identity.Name;

string connection = @"Data Source=FNUILTP10\SQL2005;Initial Catalog=RecordsManagement;Integrated Security=True";
protected void Page_Load(object sender, EventArgs e)
{


SqlConnection con = new SqlConnection(connection);
con.Open();
SqlCommand cmd = new SqlCommand("getOAs", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@OA",username));
SqlDataReader sqlReader = cmd.ExecuteReader();
//Object results;
// results = cmd.ExecuteScalar();


// txtname.text = results.ToString();
//ddcollege.DataSource = sqlReader;
if (sqlReader.HasRows)
{

sqlReader.Read();
lblname.Text = sqlReader.GetValue(0).ToString();


gvunclassified.DataSource = DataBase1.getfileRecords(username);

DataTable dataTable = gvunclassified.DataSource as DataTable;

if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
//dataView.Sort = GetSortExpression(e.SortExpression);
gvunclassified.DataSource = dataView;
gvunclassified.DataBind();

}

}

and gri<asp:GridView ID="gvunclassified" runat="server" AllowPaging="True"
AutoGenerateColumns="False" OnRowCommand="viewfiles" CellPadding="1" ForeColor="#333333"
GridLines="None" Height="190px" PageSize="5" style="margin-top: 21px; margin-left: 8px;"
Width="954px">
<alternatingrowstyle backcolor="White">
<columns>
<asp:BoundField DataField="File_Subject" HeaderText="File Subject"
SortExpression="File_Subject" />
<asp:BoundField DataField="File_Name" HeaderText="File Name"
SortExpression="File_Name" >
<itemstyle wrap="True">

<asp:BoundField DataField="UploadedOn" HeaderText="Uploaded On"
SortExpression="UploadedOn" />
<asp:ButtonField AccessibleHeaderText="View File" ButtonType="Button" CommandName="ViewFiles" ShowHeader="true"
HeaderText="View File" Text="View" />
<asp:BoundField DataField="Division/College" HeaderText="Division or College"
SortExpression="Division/College" />
<asp:TemplateField HeaderText="Department">
<itemtemplate>
<asp:DropDownList ID="DDept" runat="server"
DataSourceID="SqlDSDepartment" DataTextField="Dept_Name"
DataValueField="Dept_Name" OnSelectedIndexChanged="getddeptname" >

<asp:SqlDataSource ID="SqlDSDepartment" runat="server"
ConnectionString="<%$ ConnectionStrings:RecordsManagementConnectionString %>"
SelectCommand="SELECT DISTINCT [Dept_Name] FROM [Department]">



<asp:TemplateField HeaderText="Category">
<itemtemplate>
<asp:DropDownList ID="DDCategory" runat="server"
DataSourceID="SqlDSCategory"
DataValueField="

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