Click here to Skip to main content
16,006,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have ComboBox Dropdown i want to dispaly the selected item info to the Datagridview
Posted

1 solution

For this you need to follow this steps..


1)create and attache row data bound event to your gridview...
2) in that event find your row type like if it is datarow or edirrow or header or footer where you want to find your dropdown...
3) now cast that control to dynamic created dropdown using findcontrol method..
4) wirte the code to get the all data attach to that dropdwon
5) after this you need to get the selected dataitem from your command argument from grid event...
6) set it to the selected value of that dropdown...

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType = DataControlRowType.DataRow)
    {
        //for edit row and item template row both are under data row
        // if your dropdwon is only in edit template not in item template
        // then you need one more line to add
        // or use the code with your the if condition..
        // this is used when the dropdown is placed inside the edittemplate only
        if (GridView1.EditIndex != -1)
        {
            DropDownList ddl = (DropDownList)e.Row.FindControl("Your DROPDOWN ID");
            //Write your code to get the data filled inside the dropdown
            string selectQuery = "select * from xyztable";
            //fire this query to your database and create one data table or dataset
            DataTable dt = new DataTable();
            //fill this data table with the result of your select query
            // attache it to your drop down as it's datasource and bind the dropdown
            ddl.DataSource = dt;
            ddl.DataBind();
            //find the value from the grdiview's datasource to get that value selected to your drop down
            string selectedValue = ((DataRow)e.Row.DataItem)["YOU COLUMN NAME"].ToString();
            ddl.SelectedValue = selectedValue;
        }

    }
    if (e.Row.RowType = DataControlRowType.Header)
    {
        //write your code to find the dropdown and attchat the dataset as shown above
    }
    if (e.Row.RowType = DataControlRowType.Footer)
    {
        //write your code to find the dropdown and attchat the dataset as shown above
    }
}
 
Share this answer
 

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