我有ComboBox下拉菜单,我想将选定的项目信息分配到Datagridview [英] i have ComboBox Dropdown i want to dispaly the selected item info to the Datagridview

查看:133
本文介绍了我有ComboBox下拉菜单,我想将选定的项目信息分配到Datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ComboBox下拉菜单,我想将所选项目信息显示到Datagridview

i have ComboBox Dropdown i want to dispaly the selected item info to the Datagridview

推荐答案

为此,您需要按照以下步骤进行操作..


1)创建行数据绑定事件并将其附加到您的gridview ...
2)在这种情况下,找到您想要在其中查找下拉列表的行类型,例如datarow或edirrow还是页眉或页脚...
3)现在使用findcontrol方法将该控件转换为动态创建的下拉列表. 4)编写代码以将所有数据附加到该dropdwon
5)之后,您需要从网格事件的命令参数中获取选定的数据项...
6)将其设置为该下拉列表的选定值...

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...

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
    }
}


这篇关于我有ComboBox下拉菜单,我想将选定的项目信息分配到Datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆