在datagridview中,如何从sql server获取值并在comboxcolumn中显示? [英] In datagridview , How to get values from sql server and display in comboxcolumn ?

查看:113
本文介绍了在datagridview中,如何从sql server获取值并在comboxcolumn中显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Windows应用程序项目中有一个Grid。在Grid中,我在一列中有DROPDOWN,在该列中如何显示从SQLSERVER获取的数据。

请参阅下面..请编辑以下程序并让我解决问题..感谢提前..



connstring();

conn.Open();

adap = new SqlDataAdapter(gvretrieve_ip,conn);





ds = new System.Data.DataSet();

adap.Fill(ds,dt);



foreach(数据库DC在ds.Tables [0] .Columns)

{



}



dataGridView2.DataSource = ds.Tables [0];





int index = 5;



DataGridViewComboBoxColumn newCol = new DataGridViewComboBoxColumn();

dataGridView2.Columns.Insert(index,newCol);

dataGridView2.Columns [index] .HeaderText = dataGridView2.Columns [index + 1] .HeaderText;

newCol.MaxDropDownItems = 4;







dataGridView2.Columns.RemoveAt(index + 1);

I have a Grid in my windows application project .. In Grid , i have DROPDOWN in one column , in that column how to display data fetched from SQLSERVER .
Please see below .. please edit below program and make me to solve problem .. THANKS IN ADVANCE..

connstring();
conn.Open();
adap = new SqlDataAdapter("gvretrieve_ip", conn);


ds = new System.Data.DataSet();
adap.Fill(ds, "dt");

foreach (DataColumn DC in ds.Tables[0].Columns)
{

}

dataGridView2.DataSource = ds.Tables[0];


int index = 5;

DataGridViewComboBoxColumn newCol = new DataGridViewComboBoxColumn();
dataGridView2.Columns.Insert(index, newCol);
dataGridView2.Columns[index].HeaderText = dataGridView2.Columns[index + 1].HeaderText;
newCol.MaxDropDownItems = 4;



dataGridView2.Columns.RemoveAt(index + 1);

推荐答案

试试这段代码



Try this code

string strcon = @"Data Source=kp;Initial Catalog=Name;Integrated Security=True;Pooling=False";
        SqlConnection con;
        SqlCommand cmd;
        SqlDataAdapter da;
        DataTable dt;
        DataGridViewComboBoxColumn dgvCmb;
        public Form2()
        {
            InitializeComponent();
            grdcmd();
        }
        public void grdcmd()
        {
            con = new SqlConnection(strcon);
            con.Open();
            string qry = "Select * from Dbname";
            da = new SqlDataAdapter(qry, strcon);
            dt = new DataTable();
            da.Fill(dt);
            dgvCmb = new DataGridViewComboBoxColumn();
            foreach (DataRow row in dt.Rows)
            {
                dgvCmb.Items.Add(row["Fname"].ToString());
            }
            dataGridView1.Columns.Add(dgvCmb);
        }


查看我的文章希望这对您有所帮助。

您需要创建一个Cobobox模板列并且您可以将组合框数据源设置为sql查询。



Check my Article hope this will help you.
you need to create a Cobobox template column and you can set the combobox datasource as your sql query.

 dgvcombo.DataSource = dtsource;
 dgvcombo.DisplayMember = DisplayMember;
dgvcombo.ValueMember = ValueMember;





DataGridView Helper类 [ ^ ]


这篇关于在datagridview中,如何从sql server获取值并在comboxcolumn中显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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