How to bind Dropdown to gridview [Resolved]

Posted by Vinu under ASP.NET on 2/16/2013 | Points: 10 | Views : 5848 | Status : [Member] | Replies : 14
i wrote this code to bind in gridview...
public void ddlbindroom1()
{

DropDownList rname = (DropDownList)gdvwclass.FindControl("ddlroomname1");
string classRid1 = "SELECT RoomId,RoomName FROM Room";
DataTable dtclassRid1 = new DataTable();
OleDbCommand cmdclassRid1 = new OleDbCommand(classRid1, con);
OleDbDataAdapter daclassDid1 = new OleDbDataAdapter(cmdclassRid1);

daclassDid1.Fill(dtclassRid1);
rname.DataSource = dtclassRid1;
rname.DataTextField = "RoomName";
rname.DataValueField = "RoomId";
rname.DataBind();
rname.Items.Insert(0, new ListItem("Choose", "0"));
}

Vinu malli


Responses

Posted by: Nadh123 on: 2/17/2013 [Member] Starter | Points: 50

Up
0
Down

Resolved
Gridview Row databoubd wecan bind the dropdownlist

Mahendra
91-9908699686

Vinu, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rimi1289 on: 2/19/2013 [Member] Starter | Points: 50

Up
0
Down

Resolved
The article and codes on this link will help you.

http://www.encodedna.com/2013/02/bind-dropdownlist-gridview.htm

It will set the edited value as default text of the DrowDownList.

Rimi.

Vinu, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kusumas on: 3/1/2013 [Member] Starter | Points: 50

Up
0
Down

Resolved
Follow below code

Gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
DropDownList dropdown= (DropDownList)gv.Rows[e.NewEditIndex].FindControl("dropdown");

OleDbDataAdapter daselectRouteName1 = new OleDbDataAdapter("SELECT RoomId,RoomName FROM Room", con);

DataTable dt11 = new DataTable();
daselectRouteName1.Fill(dt11);
dropdown.DataSource = dt11;
dropdown.DataTextField = "RoomName";
dropdown.DataValueField = "RoomId";
dropdown.DataBind();
}

for Updating

Gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DropDownList dropdown= (DropDownList)gv.Rows[e.RowIndex].FindControl("dropdown");

//Write update code
}

Vinu, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 2/16/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Refer
http://www.aspdotnet-suresh.com/2012/05/bind-data-to-dropdownlist-in-gridview.html
http://www.dotnetfunda.com/articles/article989-dropdownlist-in-the-gridview-keeping-aspnet-forms-control-in-gridview-.aspx
http://www.aspnettutorials.com/tutorials/controls/dropdownlist-gridview-csharp.aspx

If this post helps you mark it as answer
Thanks

Vinu, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vinu on: 2/17/2013 [Member] Starter | Points: 25

Up
0
Down
Yes thnks alot i got the solution...but howto use it i dntno..

but i want a update funtion to use of it... if any plz...

Vinu malli

Vinu, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vinu on: 2/17/2013 [Member] Starter | Points: 25

Up
0
Down
protected void gdvwclass_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Control ctrl1 = e.Row.FindControl("ddlroomname");
Control ctrl = e.Row.FindControl("ddldeptname");//control[dropdownlist]find here
if (ctrl1 != null && ctrl != null)
{

DropDownList ddl1 = (DropDownList)ctrl1;
DropDownList ddl = (DropDownList)ctrl;

OleDbDataAdapter daselectRouteName1 = new OleDbDataAdapter("SELECT RoomId,RoomName FROM Room", con);

DataTable dt11 = new DataTable();
daselectRouteName1.Fill(dt11);
ddl1.DataSource = dt11;
ddl1.DataTextField = "RoomName";
ddl1.DataValueField = "RoomId";
ddl1.DataBind();



OleDbDataAdapter daselectRouteName = new OleDbDataAdapter("SELECT DepartmentId, DepartmentName FROM Department", con);

DataTable dt1 = new DataTable();
daselectRouteName.Fill(dt1);
ddl.DataSource = dt1;
ddl.DataTextField = "DepartmentName";
ddl.DataValueField = "DepartmentId";
ddl.DataBind();
}



i did as u suggested..
but no i want to update it ..how..?

Vinu malli

Vinu, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vinu on: 2/18/2013 [Member] Starter | Points: 25

Up
0
Down
i want to set edited value to it ..as it was previous value....what to do...?

Vinu malli

Vinu, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vinu on: 3/1/2013 [Member] Starter | Points: 25

Up
0
Down
Stilll i not got the problem

Vinu malli

Vinu, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vinu on: 3/1/2013 [Member] Starter | Points: 25

Up
0
Down
K...5n...i m going to try now... wait..

nthnx if it works...

Vinu malli

Vinu, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kusumas on: 3/1/2013 [Member] Starter | Points: 25

Up
0
Down
if it is useful to you mark as answer............

Vinu, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vinu on: 3/1/2013 [Member] Starter | Points: 25

Up
0
Down
k..

Vinu malli

Vinu, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vinu on: 3/13/2013 [Member] Starter | Points: 25

Up
0
Down
Thnx...

Vinu malli

Vinu, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kusumas on: 3/13/2013 [Member] Starter | Points: 25

Up
0
Down
thnx..............

Vinu, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 3/13/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi
try this code

Client Side
===============
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ddlCity" runat ="server" DataTextField="StudentName" DataValueField ="StudentId" ></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>

Server Side
===============
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

Dt = ReturnDt("Select StudentId,StudentName from dbo.Table_1");
if (e.Row.RowType == DataControlRowType.DataRow)
{
var ddl = (DropDownList)e.Row.FindControl("ddlCity");
BindDddl(ddl,e);
}
}

public void BindDddl(DropDownList ddl, GridViewRowEventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
//int CountryId = Convert.ToInt32(e.Row.Cells[1].Text);
SqlCommand cmd = new SqlCommand("Select StudentId,StudentName from dbo.Table_1", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet dt = new DataSet();
da.Fill(dt);
con.Close();
ddl.DataSource = dt;
ddl.DataTextField = "StudentName";
//if (CountryId == 2) { ddl.SelectedValue = "2"; }
ddl.DataValueField = "StudentId";
ddl.DataBind();
ddl.Items.Insert(0, new ListItem("--Select--", "0"));

}


Mark as Answer if its helpful to you

Vinu, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response