2013-09-10 41 views
1

如何做好這項工作對gridviewcomboboxcolumns任何想法PLXdatagridviewcombobox列在另一列依賴

//窗體加載事件

string query="select article_name from article"; 
SqlCommmand cmd = new SqlCommand(query,con); 
SqlDataAdapter da= new SqlDataAdapter(cmd); 
DataTable dt=new DataTable(); 
da.Fill(dt); 
combobox1.items.clear(); 
for(int i=0;i<dt.rows.count;i++) 
{ 
combobox1.items.add(dt.rows[i].cells[0].toString()); 
} 

\ ComboBox1選擇IndexChange事件

string query1="select description from article where article_name='"+combobox1.selectedItem.ToString()+"'"; 
SqlCommmand cmd1 = new SqlCommand(query1,con); 
SqlDataAdapter da1= new SqlDataAdapter(cmd); 
DataTable dt1=new DataTable(); 
da1.Fill(dt1); 
combobox2.items.clear(); 
for(int i=0;i<dt1.rows.count;i++) 
{ 
combobox2.items.add(dt1.rows[i].cells[0].toString()); 
} 

\現在假設這些2 COMBOX是gridviewCombobox列因此如何使 上gridviewcombobox列這項工作

Project in Windows Form in C# 
+0

同樣的問題,兄弟,我也需要這個 – naeemshah1

回答

0

餘米張貼這answer後幾個月,因爲它有助於對 thoose whoose面臨的問題DataGridviewComboboxcell 我做了我自己的技能首先填寫我的第一個/主列

SqlCommand objCmd = new SqlCommand("select distinct article_name from Setup_article_custominvoice", con); 
        SqlDataAdapter objDA = new SqlDataAdapter(objCmd); 
        objDA.SelectCommand.CommandText = objCmd.CommandText.ToString(); 
        DataTable dt = new DataTable(); 
        objDA.Fill(dt); 
        article.DataSource = dt; 
        //this column1 will display as text 
        article.DisplayMember = "article_name"; 

後,我要去上Cell End Edit

if (dataGridView1.CurrentCell == dataGridView1.CurrentRow.Cells["article_name"]) 
       { 
        string CategoryValue = ""; 
        //string CategoryValue1 = ""; 

        if (dataGridView1.CurrentCell.Value != null) 
        { 
         CategoryValue = dataGridView1.CurrentCell.Value.ToString(); 
         //CategoryValue1 = dataGridView1.CurrentCell.Value.ToString(); 
        } 
        //SqlConnection objCon = new SqlConnection(@"Data Source=.\SqlExpress;Initial Catalog=dbTest3;Integrated Security=True"); 
        string query = "select article_name,composition from Setup_article_custominvoice where article_name='" + CategoryValue + "'"; 
        SqlCommand objCmd = new SqlCommand(query, con); 

        SqlDataAdapter objDA = new SqlDataAdapter(objCmd); 

        objDA.SelectCommand.CommandText = objCmd.CommandText.ToString(); 
        DataTable dt = new DataTable(); 

        objDA.Fill(dt); 

        DataGridViewComboBoxCell t = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[2] as DataGridViewComboBoxCell; 
        t.DataSource = dt; 
        t.DisplayMember = "composition"; 

       }