从datagridview的下拉列表中选择数据后,如何将数据显示到文本框中 [英] How can we display a data to a textbox after selecting it from a dropdown list in a datagridview

查看:180
本文介绍了从datagridview的下拉列表中选择数据后,如何将数据显示到文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是Windows应用程序的初学者,我需要帮助。



在我的DataGridView中有一个Windows应用程序是多列,其中一列是下拉列表,我必须从中选择一个值,在选择一个值后,其后续数据必须显示在同一行中,即文本框中。此外,用户还可以编辑文本框中的可用数据。并且,动态地必须为同一目的创建下一行。

我在第一行的下拉列表中获取值,并且还创建了动态下一行,但没有在文本框中填充数据,我没有得到下拉列表中下一行的值,而是我得到的输入字符串的格式不正确。



请有查看代码,让我知道我哪里错了。



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.

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 !!

推荐答案

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;
      }
}


我的代码中没有任何错误。



请在网格中添加两列



1.组合框列

2.文本框列(名称:ColText)



再试一次......
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...


对不起亲爱的,我不明白你这样的问题。



请提供您的代码,我会查看此事并作出相应的回应......
Sorry dear, i can''t understand your problem like this.

Please provide your code, i will look this matter and will respond accordingly...


这篇关于从datagridview的下拉列表中选择数据后,如何将数据显示到文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆