Subscribe to Subscribers
Talk to Webmaster Tony John

Online Members

Asheej T K
Gopi
More...

Forums » .NET » .NET »

Need code for dropdown in grid


Posted Date: 24 Jun 2012      Posted By:: asdfg     Member Level: Gold    Member Rank: 952     Points: 2   Responses: 4



Need dropdown as control in the grid view
as there is 1 table school --> id, name, code
another table year--> id, name, code
and other table schoolyear --> id, schoolid, yearid

i want to show school name, school code, and year as dropdown




Responses

#677153    Author: asdfg      Member Level: Gold      Member Rank: 952     Date: 24/Jun/2012   Rating: 2 out of 52 out of 5     Points: 1




 
#677159    Author: Pawan Awasthi      Member Level: Diamond      Member Rank: 4     Date: 24/Jun/2012   Rating: 2 out of 52 out of 5     Points: 4

Hai Phani,
First you can take the dropdown lists as per your requirements and then you need to fill them.

<asp:TemplateField HeaderText="Products">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>

Now populate the dropdown list. For populating the dropdown lists, you can use the event handler RowDataBound as:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataTable myTable = new DataTable();
DataColumn id= new DataColumn("ID");
DataColumn name= new DataColumn("Name");
myTable.Columns.Add(id);
myTable.Columns.Add(name);
...
..
.

You can go through the below link for more detailed explanation with the example which is well suited to your requirements:

http://highoncoding.com/Articles/169_DropDownList_Inside_GridView__Method_1_.aspx

Let us know if you don't understand anywhere.
Hope it will be helpful to you.

Regards,
Pawan Awasthi(DNS MVM)
+91 8143683708 (pawansoftit@gmail.com)
Outstanding Contribution Award..NTT Data Inc



 
#677169    Author: Anil Kumar Pandey      Member Level: Platinum      Member Rank: 1     Date: 25/Jun/2012   Rating: 2 out of 52 out of 5     Points: 4

Place the Drop down in the Template field of the Grid, make use of the Row data bound event to bind the element to it as


void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{

if(e.Row.RowType == DataControlRowType.DataRow)
{
label lbl = (label) e.Row.FindControl("lblHidden");
DropDownList ddl = (label) e.Row.FindControl("ddlID");

if(lbl.Text =="A")
{
ddl.Items.Clear();
ddl.Items.Add("B");
ddl.Items.Add("C")
}
Else if(lbl.Text =="B")
{
ddl.Items.Clear();
ddl.Items.Add("C")
}
}

}


Thanks & Regards
Anil Kumar Pandey
Microsoft MVP, DNS MVM






 
#677176    Author: RayalaHariKishore      Member Level: Gold      Member Rank: 52     Date: 25/Jun/2012   Rating: 2 out of 52 out of 5     Points: 4

hi,

we will add controls to gridview like this within <columns> you must add


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="name">
<ItemTemplate>
<asp:DropDownList ID="dropdownlist1" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


here it will create dropdownlist

now write rowdatabound to bind data to it

protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = getdataset().Tables[0];
GridView1.DataBind();
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

DataTable mytable = new DataTable();
DataColumn productidcolumn = new DataColumn("productid");
DataColumn productnamecolumn = new DataColumn("productname");

mytable.Columns.Add(productidcolumn);
mytable.Columns.Add(productnamecolumn);

DataSet ds = new DataSet();
ds = getdataset();
int categoryid = 0;
string expression = string.Empty;

if (e.Row.RowType == DataControlRowType.DataRow)
{
categoryid = Int32.Parse(e.Row.Cells[0].Text);
expression = "categoryid = " + categoryid;
DropDownList ddl = (DropDownList)e.Row.FindControl("dropdownlist1");
DataRow[] rows = ds.Tables[0].Select(expression);

foreach (DataRow row in rows)
{
DataRow newrow = mytable.NewRow();
newrow["productid"] = row["productid"];
newrow["productname"] = row["productname"];
mytable.Rows.Add(newrow);
}
ddl.DataSource = mytable;
ddl.DataTextField = "productname";
ddl.DataValueField = "productid";
ddl.DataBind();
}


}

private DataSet getdataset()
{
string connectionstring = "Data Source=localhost;Initial Catalog=northwind;User ID=sa;password=";
string query = "select * from table";
SqlConnection myconnection = new SqlConnection(connectionstring);
SqlDataAdapter ad = new SqlDataAdapter(query, myconnection);
DataSet ds = new DataSet();
ad.Fill(ds);
return ds;
}


refer this and modify according to you.

Rayala HariKishore

try..try..try...you achieved it.
http://rayalaharikishore.wordpress.com/



 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Next : Convert C# “is” Operator to “instanceof” Operator in Java
Previous : IP Ping Issue in Server
Return to Discussion Forum
Post New Message
Category:

Related Messages
Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2012 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.