Click here to Skip to main content
12,762,643 members (30,362 online)
Rate this:
Please Sign up or sign in to vote.
See more: windows.forms
I am a beginner in windows application and I need a help.

In my DataGridView of a windows application there are multi columns out of which one is dropdownlist from which I've to select a value and after selecting a value its subsequent data must be displayed in the same row i.e in the textbox.Also user can edit the data available in the textbox.And, dynamically the next row must be created for the same purpose.
I am getting the values in dropdown list of first row and dynamically next row is also created but not getting the data filled in the textboxes, neither I'm getting values for the next row in the dropdown list, rather I'm getting an exception of Input string not in correct format.

Please have a look at the code and let me know where am I going wrong.

private void comboBox_SelectedIndexChanged(object sender, EventArgs e)


            DataGridViewComboBoxColumn dgvComboBox;

            DataGridViewTextBoxColumn dgTxtBox;

            dgvc = (DataGridViewComboBoxColumn)dataGridInvoice.Columns["dgvProductCode"];

            int indexRow = dgvc.Index;            

            dgTxtBox = (DataGridViewTextBoxColumn)dataGridInvoice.Columns["dgvProductName"];

            dgTxtBox = (DataGridViewTextBoxColumn)dataGridInvoice.Columns["dgvPriceColumn"];

            dgTxtBox = (DataGridViewTextBoxColumn)dataGridInvoice.Columns["dgvQuantityColumn"];

            //dgvc = (DataGridViewTextBoxColumn)dataGridInvoice.Columns["dgvTotalColumn"];

            string strText = dgvc.Selected.ToString();

            string strText1 = dgvc.ValueMember.ToString();

            string SelectedText = Convert.ToString((dataGridInvoice.Rows[indexRow].Cells["dgvProductCode"] as DataGridViewComboBoxCell).FormattedValue.ToString());

            int SelectedVal = Convert.ToInt32(dataGridInvoice.Rows[indexRow].Cells["dgvProductCode"].Value); //Format Exception 

            string SelectedText1 = Convert.ToString((dataGridInvoice.Rows[indexRow].Cells["dgvProductCode"] as DataGridViewComboBoxCell).FormattedValue.ToString());

            int SelectedVal1 = Convert.ToInt32(dataGridInvoice.Rows[indexRow].Cells["dgvProductCode"].Value);         



            ///get the id from strText1 and pass it to the procedure along with command type 'Id' to fetch data of name and price of product

 DataTable dt_ProductCategory = b.BindInvoiceDataGridView(strText1, "Id");

            DataGridViewTextBoxColumn dgTxtProductName = (DataGridViewTextBoxColumn)dataGridInvoice.Columns["dgvProductName"];

            //dgTxtProductName.DataSource = dt_ProductCategory


        public DataTable BindInvoiceDataGridView(string id,string commandtype)



            cmd = new SqlCommand("sp_InvoiceDataGridViewDisplay",con);

            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@prodId", id);

            cmd.Parameters.AddWithValue("@commandType", commandtype);

                      SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmd);

            DataTable dtInvoiceDataGridViewDisplay = new DataTable();



            return dtInvoiceDataGridViewDisplay;


    public void BindingMethod()
            DataTable dt1 = b.BindInvoiceDataGridView("", "All");
            DataGridViewComboBoxColumn cbxColumn = dataGridInvoice.Columns["dgvProductCode"] as DataGridViewComboBoxColumn;
            cbxColumn.DataSource = dt1;
            cbxColumn.DisplayMember = "code";
            cbxColumn.ValueMember = "prod_id";

How to display the data in the row i.e in the text box and for the next row in the drop down list where am I going wrong ( where to make changes) ?

Please Help me !!
Posted 27-Dec-12 19:49pm
Hetal Jariwala 28-Dec-12 1:14am
Check if
int indexRow = dgvc.Index;
not return -1 value
rabbil 28-Dec-12 1:35am
Its returning only 0 as its value.
check it after various experiments and debugging also,still not able to find out.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

private void grd_CellValueChanged(object sender, DataGridViewCellEventArgs e)
      if (e.ColumnIndex == 0) // COLUMN WHICH HAS COMBO BOX
          DataGridViewComboBoxCell cbCell = (DataGridViewComboBoxCell)grd.Rows[e.RowIndex].Cells[e.ColumnIndex];

          DataGridViewTextBoxCell txCell = (DataGridViewTextBoxCell)grd.Rows[e.RowIndex].Cells["ColText"];

          txCell.Value = cbCell.Value;
rabbil 28-Dec-12 2:21am
Its not working, throwing an exception: argument out of range.what else can be done ?
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

There is no any error in my code.

Please add two columns in grid

1. combobox column
2. textbox column (Name : "ColText")

and try again...
rabbil 28-Dec-12 2:51am
Ofcourse, your code is right but in my case while this event is generated its throwing an exception due to which the solution is not able to run with my other event.
Should i use only this event or something else also.
please help me out.
Thanks !!
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Sorry dear, i can't understand your problem like this.

Please provide your code, i will look this matter and will respond accordingly...
Sandeep Mewara 28-Dec-12 9:53am
Why are you posting comments as solutions one after the other?

Use comment feature to interact or update your answer.
rabbil 29-Dec-12 0:42am
Thanks for your efforts !! And whatever code I'm using for DataGridView I've provided already. And if you have interest on this anymore then you can solve this requirement in a simple grid itself using DropDown in one column and textbox in remaining 3 or 4 columns where the textboxes must be populated after selecting any values from dropdown list among one of the column of the same grid.
Or, if possible please message me your mail id so that i can send you the whole code.
Thanks And Regards,
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

Dear you have use event comboBox_SelectedIndexChanged

but i suggest to write my code on grd_CellValueChanged

try again

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 29 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100