Fill Dropdown list on GridView1_RowDataBound

Finding controls on SelectedIndexChanged Events


Create Table Here I Have Provided The Script

CREATE TABLE [dbo].[Food](

[Fid] [bigint] primary key IDENTITY(1,1) NOT NULL,

[Fname] [nvarchar](100) NULL,

[Fprice] [bigint] NULL,

[Recstatus] [char](1) NULL,

)


INSERT INTO Food(Fname,Fprice,Recstatus)VALUES('Dal',2000,'A')

INSERT INTO Food(Fname,Fprice,Recstatus)VALUES('Rice',52000,'A')

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Maingrid.aspx.cs" Inherits="ALLAboutGridview.Maingrid" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

<title></title>

</head>

<body>

<form id="form1" runat="server">

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"

GridLines="Horizontal" OnRowDataBound="GridView1_RowDataBound" BackColor="White"

BorderColor="#336666" BorderStyle="Double" BorderWidth="3px">

<Columns>

<asp:TemplateField HeaderText="Product">

<ItemTemplate>

<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" Width="150px" runat="server"

AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">

<asp:ListItem Text="Select" Value="0"></asp:ListItem>

</asp:DropDownList>

<br />

<asp:Label ID="label1" BackColor="BurlyWood" runat="server" Text=""></asp:Label>

</ItemTemplate>

</asp:TemplateField>

</Columns>

<FooterStyle BackColor="White" ForeColor="#333333" />

<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />

<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />

<RowStyle BackColor="White" ForeColor="#333333" />

<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />

<SortedAscendingCellStyle BackColor="#F7F7F7" />

<SortedAscendingHeaderStyle BackColor="#487575" />

<SortedDescendingCellStyle BackColor="#E5E5E5" />

<SortedDescendingHeaderStyle BackColor="#275353" />

</asp:GridView>

</form>

</body>

</html>

public void bindgrid()

{

SqlCommand cmd = new SqlCommand("select * from food ", con);

cmd.CommandType = CommandType.Text;

SqlDataAdapter da = new SqlDataAdapter();

da.SelectCommand = cmd;

DataTable dt = new DataTable();

da.Fill(dt);

if (dt.Rows.Count > 0)

{

GridView1.DataSource = dt;

GridView1.DataBind();

}

else

{

DataRow dr = dt.NewRow();

dt.Rows.Add(dr);

GridView1.DataSource = dt;

GridView1.DataBind();

}

}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

SqlCommand cmd = new SqlCommand("select * from food ", con);

cmd.CommandType = CommandType.Text;

SqlDataAdapter da = new SqlDataAdapter();

da.SelectCommand = cmd;

DataTable dt = new DataTable();

da.Fill(dt);

if (dt.Rows.Count > 0)

{

DropDownList DropDownList1 =

(DropDownList)e.Row.FindControl("DropDownList1");

DropDownList1.DataSource = dt;

DropDownList1.DataTextField = "Fname";

DropDownList1.DataValueField = "Fid";

DropDownList1.DataBind();

}

}

}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

{

DropDownList drp = (DropDownList)sender;

GridViewRow gv = (GridViewRow)drp.NamingContainer;

int index = gv.RowIndex;

DropDownList DropDownList1 = (DropDownList)GridView1.Rows[index].FindControl("DropDownList1");

Label label1 = (Label)GridView1.Rows[index].FindControl("label1");

label1.Text = DropDownList1.SelectedItem.Text;

}

Final Output