C# .NET - DATAGRIDVIEW COMBO BOX COLUMN NEED HELP BADLY!!!

Asked By Warjie Malibago on 09-Jun-11 12:37 AM
i have this code for the first comboBox column,

//add category

private void AddCategory()

{

  CommandLine ccon = new CommandLine();

  OleDbConnection con = new OleDbConnection(ccon.constr);

  con.Open();

  string q = "select DISTINCT CategoryName from Category";

  OleDbDataAdapter da = new OleDbDataAdapter(q, con);


  DataTable
dt = new DataTable();

  da.Fill(dt);


  //Create combo column

  DataGridViewComboBoxColumn
combo1 = new DataGridViewComboBoxColumn();

  
  //Assing dataSource








  combo1.DataSource = dt;

  //Assing table Field on combo

  combo1.ValueMember = "CategoryName";

  combo1.DisplayMember = "CategoryName";

  //Adding combo box column in dataGridView



  dataGridView1.Columns.Add(combo1);

  con.Close();

}

and this is my code for the 2nd comboBox column,

//add product

private void AddProduct()

{

  string q = "SELECT DISTINCT Product.ProductName FROM Category INNER JOIN Product ON Category.CategoryID = Product.CategoryID WHERE Category.CategoryName ='" + dataGridView1.Rows[0].Cells[0].ToString() + "'";

  OleDbDataAdapter da = new OleDbDataAdapter(q, constr);

 

  DataTable dt = new DataTable();

  da.Fill(dt);


  //Craete combo column

  DataGridViewComboBoxColumn combo1 = new DataGridViewComboBoxColumn();


  //Assing dataSource

  combo1.DataSource = dt;


  //Assing table Field on combo

  combo1.ValueMember = "ProductName";

  combo1.DisplayMember = "ProductName";


  //Adding combo box column in dataGridView

  dataGridView1.Columns.Add(combo1);

}

what i want here is to show values on the 2nd column depending on the chosen value in the 1st column...but values on the 2nd column don't appear..

For example, i choose Food on the 1st column,what i want is to show only products under Food on the 2nd column,but no values appear in the 2nd column, i put ShowCategory method on the Form_Load but i don't know where to put the ShowProduct method



















Ravi S replied to Warjie Malibago on 09-Jun-11 12:50 AM
HI

try this

<ajaxToolkit:ComboBox ID="ComboBox1" runat="server"
   AutoPostBack="True"
   DropDownStyle="DropDown"
   AutoCompleteMode="Suggest"
   CaseSensitive="False"
   CssClass=""
   ItemInsertLocation="Append" ... >


refer the link for examples
http://www.asp.net/ajax/ajaxcontroltoolkit/samples/combobox/combobox.aspx
http://office.microsoft.com/en-us/access-help/basing-one-combo-box-on-another-HA001173058.aspx
http://accessprogrammer.blogspot.com/2005/08/filter-combobox-based-on-another.html
Warjie Malibago replied to Ravi S on 09-Jun-11 12:52 AM
ahh,,i'm not using ASP.NET...but thanks anyway..: )
Kirtan Patel replied to Warjie Malibago on 09-Jun-11 01:14 AM
you can do it like following code

DataGridViewComboBoxCell dgvc = DataGridView1.Rows[0].Cells[2];
if (DataGridView1.Rows[0].Cells[1].Value == "Jack")
{
	dgvc.Items.Add("Fe");
	dgvc.Items.Add("Fi");
} 
else (DataGridView1.Rows[0].Cells[3].Value == "Giant") 
{
	dgvc.Items.Add("Fo");
	dgvc.Items.Add("Fum");
}

Anoop S replied to Warjie Malibago on 09-Jun-11 01:25 AM
You need sub catagory of 1st combobox in 2nd combobox. right?

Then in the in the selected index change of 1st combobox write the query for selecting data, and change the query to ;ilr this way

string q = "SELECT DISTINCT Product.ProductName FROM Category INNER JOIN Product ON Category.CategoryID = Product.CategoryID WHERE Category.CategoryName ='"  + comboBox1.Text + "'";

comboBox1.Text (category combobox name) means you will get the selected value in the category
 
Warjie Malibago replied to Anoop S on 09-Jun-11 02:01 AM
ah,I'm using a datagridview ComboBox Column here,,what event in datagridview should i use to run the code for AddProduct method?
Anoop S replied to Warjie Malibago on 09-Jun-11 02:50 AM
You can write code in EditingControlValueChanged ->The EditingControlValueChanged property is set to true to specify that the owning cell's value has changed, and the cell may need to be updated to reflect this change. The owning cell or table will reset this property to false after it has updated itself.

refer this for EditingControlValueChanged
http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxeditingcontrol.aspx

It shows how to connect to the SelectedIndexChanged event on the
combobox that is in the grid.