Click here to Skip to main content
15,921,452 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a datagridview which is bound to the datatable(dtDataCSV). This datatable is filled with values from the excel file which has only 5 col-Date,Bank Desc,Dr,Cr,Bal . dtDataCSV has 7 columns. I have inserted a first combobox at column 5 in datagridview. Now if i select-say third item of the first combobox...then it should add another combobox and fill the corresponding items.

How do i do that?
Below is my code
private void txtSheet_Leave(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtFileName.Text))
                {
                    MessageBox.Show("Please select file first");
                    return;
                }
                if (string.IsNullOrEmpty(txtSheet.Text))
                {
                    MessageBox.Show("Please write the exact sheet name of excel file");
                    return;
                }

                dtDataCSV = new DataTable();
                DataRow dr;
                dtDataCSV.Columns.Add("TranRefNo", typeof(int));
                dtDataCSV.Columns.Add("Date", typeof(DateTime));
                dtDataCSV.Columns.Add("Bank Desc/Supplier Name", typeof(string));
                dtDataCSV.Columns.Add("Account Code", typeof(string));
                dtDataCSV.Columns.Add("Debit", typeof(double));
                dtDataCSV.Columns.Add("Credit", typeof(double));
                if (comboBox1.Text == "Bank Account")
                {
                    dtDataCSV.Columns.Add("Balance", typeof(string));
                }
                

             

                filHandCSV = new clsFileHandler(txtFileName.Text);

                if (filHandCSV.FileInf.Exists)
                {
                    var connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", filHandCSV.FileInf);
                    OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [" + txtSheet.Text.Trim().ToString() + "$]", connectionString);
                    DataSet ds = new DataSet();
                    adapter.Fill(ds, "dtref");
                    DataTable dttable = ds.Tables["dtref"];

                    //Load CSV file to dtDataCSV
                    int TranRefNo = 1;
                    if (dttable.Rows.Count > 0)
                    {
                       
                            foreach (DataRow dr1 in dttable.Rows)
                            {
                                // if (dr1[1].ToString() != "" && dr1[2].ToString() != "")
                                if (dr1[1].ToString() != "")
                                {
                                    dr = dtDataCSV.NewRow();
                                    dr["TranRefNo"] = TranRefNo;
                                    dr["Date"] = Convert.ToDateTime(dr1[0].ToString()).ToString("dd MMM yyyy");
                                    dr["Bank Desc/Supplier Name"] = dr1[1];
                                    dr["Debit"] = dr1[2];
                                    dr["Credit"] = dr1[3];
                                    dr["Balance"] = dr1[4];
                                    TranRefNo = TranRefNo + 1;
                                    dtDataCSV.Rows.Add(dr);
                                }
                            }
                        
                        
                    }


                    TranRefNo = 1;
                 

                    dgvLoadExp.DataSource = dtDataCSV;
                    //Formats the date column
                    dgvLoadExp.Columns[1].DefaultCellStyle.Format = "dd-MMM-yyyy";

                    // Add a new Column (Account Head) 
                    DataGridViewComboBoxColumn colAccHead = new DataGridViewComboBoxColumn();
                    Expenses objAccHead = new Expenses();
                    
                    DataTable dt = new DataTable();
                    dt.Columns.Add("catid", typeof(int));
                    dt.Columns.Add("Account Head", typeof(string));
                    dr = dt.NewRow();
                    dr["catid"] = -1;
                    dr["Account Head"] = "";
                    dt.Rows.Add(dr);
                    dt.Merge(objAccHead.getAccountHead());
                    
                    colAccHead.DataSource = dt;
                    colAccHead.HeaderText = "Account Head";
                    colAccHead.DisplayMember = "Account Head";
                    colAccHead.ValueMember = "catid";
                    this.dgvLoadExp.Columns.Insert(4, colAccHead);
                    
                    //Use DataPropertyName to get the selected key column as in :- 
                    this.dgvLoadExp.Columns[4].DataPropertyName = "catid";
                  
                }

            }
            catch (Exception ex)
            {

            }
        }

        private void dgvLoadExp_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (dgvLoadExp.CurrentCell.ColumnIndex == 4)
            {
                ComboBox cmb = e.Control as ComboBox;
                cmb.SelectedValueChanged += new EventHandler(cmb_SelectedValueChanged);
            }
        }

        void cmb_SelectedValueChanged(object sender, EventArgs e)
        {
            DataGridViewComboBoxColumn colSubAccHead = new DataGridViewComboBoxColumn();

            Expenses objSubAccHead = new Expenses();
            colSubAccHead.DataSource = objSubAccHead.getSubAccountHead(((ComboBox)sender).SelectedValue.ToString());//Error at this line
            colSubAccHead.HeaderText = "Sub Account Head";
            colSubAccHead.DisplayMember = "SubHead";
            colSubAccHead.ValueMember = "id";
            this.dgvLoadExp.Columns.Insert(5, colSubAccHead);
           
        }

objSubAccHead.getSubAccountHead() returns a datatable whose AccountHead id is selected value,i.e catid

Any help would be really appreciated.
Thanks
Posted

Hi
Adding second combobox next to first combobox in its selectedvaluechanged event ...continuously adds comboboxes. But i need only two comboboxes.So what i have done is added second combobox in txtsheet_Leave event.

Here is the code.
C#
DataGridViewComboBoxColumn colSubAccHead = new DataGridViewComboBoxColumn();
private void txtSheet_Leave(object sender, EventArgs e)
{
 ......

                     dgvLoadExp.DataSource = dtDataCSV;
                    //Formats the date column
                    dgvLoadExp.Columns[1].DefaultCellStyle.Format = "dd-MMM-yyyy";
 
                    // Add a new Column (Account Head) 
                    DataGridViewComboBoxColumn colAccHead = new DataGridViewComboBoxColumn();
                    Expenses objAccHead = new Expenses();
                    
                    DataTable dt = new DataTable();
                    dt.Columns.Add("catid", typeof(int));
                    dt.Columns.Add("Account Head", typeof(string));
                    dr = dt.NewRow();
                    dr["catid"] = -1;
                    dr["Account Head"] = "";
                    dt.Rows.Add(dr);
                    dt.Merge(objAccHead.getAccountHead());
                    
                    colAccHead.DataSource = dt;
                    colAccHead.HeaderText = "Account Head";
                    colAccHead.DisplayMember = "Account Head";
                    colAccHead.ValueMember = "catid";
                    this.dgvLoadExp.Columns.Insert(4, colAccHead);
                    
                  
//Use DataPropertyName to get the selected key column as in :- 
                    this.dgvLoadExp.Columns[4].DataPropertyName = "catid";
                    
//Added second combobox here
                    colSubAccHead = new DataGridViewComboBoxColumn();
                    colSubAccHead .HeaderText = "Sub Account Head";
                    colSubAccHead .DisplayMember = "SubHead";
                    colSubAccHead .ValueMember = "id";
                    this.dgvLoadExp.Columns.Insert(5, colSubAccHead);
                    this.dgvLoadExp.Columns[5].DataPropertyName = "id";

}
  private void dgvLoadExp_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (dgvLoadExp.CurrentCell.ColumnIndex == 4)
            {
                ComboBox cmb = e.Control as ComboBox;
                cmb.SelectedValueChanged += new EventHandler(cmb_SelectedValueChanged);
            }
        }
void cmb_SelectedValueChanged(object sender, EventArgs e)
        {
            Combobox cmb=(ComboBox)sender;
            if(cmb.SelectedValue==null) return;
            Expenses objSubAccHead = new Expenses();
            colSubAccHead.DataSource=objSubAccHead.getSubAccountHead(cmb.SelectedValue.ToString());
           
           
        }


It shows correct values.
But if the user selects both comboboxes and then changes the first so that the second is no longer valid.
Then it shows an error:
System.ArgumentException:DataGridViewComboBoxCell value is not valid
To Replace this default dialog please handle DataError Event.

What should i do? Any Help would be appreciated.
Thanks
 
Share this answer
 
v2
 
Share this answer
 
Comments
samip shrestha 14-Sep-11 6:44am    
Hi MaulikDusara
Actually i am developing a window application. Link you provided is for asp.net.

So if you have any idea about the window application. Would be thankful to you.

Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900