Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
646 views
in Technique[技术] by (71.8m points)

c# - How to bind data in datagridview combobox column

Here are contacts but we can have multiple contacts so i want to show a list in combobox

DataTable dt = new DataTable();
dt = MainClass.GetDatabyQuery("select * from tbl");

if (dt.Rows.Count > 0)
{
    dgv_ClientDetail.DataSource = dt;
}

I have this method to fetch values from database in datagridview but I want one datagridview combobox column and want to bind data in one dgv combobox and other in dgv texboxes. If anybody know then tell me. Here are three columns Name, City , Contacts. I want to show multiple contacts in dgv combobox column

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just select only Name and City in dt, so that you can do something like,

        dgv_ClientDetail.DataSource = dt;
        DataGridViewComboBoxColumn dgvCboColumn = new DataGridViewComboBoxColumn();
        dgvCboColumn.Name = "Contacts";
        dgvCboColumn.DataSource = dtContacts; //DataTable that contains contact details
        dgvCboColumn.DisplayMember = "Name";
        dgvCboColumn.ValueMember = "Id";
        dataGridView1.Columns.Add(dgvCboColumn);

EDIT:

        dgv_ClientDetail.DataSource = new DataView(dt)
                                         .ToTable(true, new string[] { "Name", "City" });
        DataGridViewComboBoxColumn dgvCboColumn = new DataGridViewComboBoxColumn();
        dgvCboColumn.Name = "Contacts";
        dgv_ClientDetail.Columns.Add(dgvCboColumn);
        foreach (DataGridViewRow row in dgv_ClientDetail.Rows)
        {
            DataGridViewComboBoxCell cboContacts = (DataGridViewComboBoxCell)
                                                         (row.Cells["Contacts"]);
            cboContacts.DataSource = //Get the contact details of a person,
                                     //using his Name or Id field (row.Cells["Name"]);
            cboContacts.DisplayMember = "Name"; //Name column of contact datasource
            cboContacts.ValueMember = "Id";//Value column of contact datasource
        }

Hope this helps...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...