Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
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.

C#
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);

           ///

           ///Summary

           ///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)

       {

           con.Open();

           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();

           sqlDataAdapter.Fill(dsInvoiceDataGridViewDisplay);

           con.Close();

           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
Comments
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.

C#
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;
      }
}
 
Share this answer
 
Comments
Rabbil 28-Dec-12 2:21am    
Its not working, throwing an exception: argument out of range.what else can be done ?
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...
 
Share this answer
 
Comments
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 !!
Sorry dear, i can't understand your problem like this.

Please provide your code, i will look this matter and will respond accordingly...
 
Share this answer
 
Comments
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,
Rabbil
Dear you have use event comboBox_SelectedIndexChanged

but i suggest to write my code on grd_CellValueChanged

try again
 
Share this answer
 
v2

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