双击dataGridView后的数据传递示例

 该示例实现的功能为:
点击form1窗体上的comboBox下拉列表框然后弹出一个form2窗体,form2窗体上有一个dataGridView1,然后双击dataGridView1的某一行数据,会把选中的该行数据传递到form1,并自动填充form1中的comboBox和textbox.

form2窗体中的主要事件代码为:
// 填充dataGridView1
public   void  datalist()
        
{

            
string mysql = "select papertypes as 用纸类型,len as 长,wid as 宽,ke as 克重,xishu as 系数,dprice as 吨价,lprice as 令价 from system_paperprice";
            DataSet grid 
= new DataSet();
            
string connectionString = ConfigurationManager.ConnectionStrings["进销存管理系统.Properties.Settings.ld_ysglConnectionString"].ConnectionString;
            SqlConnection myConnection 
= new SqlConnection(connectionString);
            myConnection.Open();

            grid.Clear();

            SqlDataAdapter myda 
= new SqlDataAdapter(mysql, myConnection);
            myda.Fill(grid);
            dataGridView1.DataSource 
= grid.Tables[0];
            myConnection.Close();

        }



        
// 双击选择用纸类型信息并回送到调用的窗体-------------
         private   void  dataGridView1_DoubleClick( object  sender, System.EventArgs e)
        
{
            
if (this.dataGridView1.ReadOnly == true
            
{
                    
int intCurrentRowNumber = this.dataGridView1.CurrentCell.RowIndex;
                    
string sendStokername, sendStokerdprice, sendStokerke, sendStokerxishu, sendStokerlprice;
                    sendStokername 
= this.dataGridView1.Rows[intCurrentRowNumber].Cells[0].Value.ToString().Trim();
                    sendStokerdprice 
= this.dataGridView1.Rows[intCurrentRowNumber].Cells[5].Value.ToString().Trim();
                    sendStokerke 
= this.dataGridView1.Rows[intCurrentRowNumber].Cells[3].Value.ToString().Trim();
                    sendStokerxishu 
= this.dataGridView1.Rows[intCurrentRowNumber].Cells[4].Value.ToString().Trim();
                    sendStokerlprice 
= this.dataGridView1.Rows[intCurrentRowNumber].Cells[6].Value.ToString().Trim();

                    
string[] sendArray = new string[] { sendStokername, sendStokerdprice, sendStokerke, sendStokerxishu, sendStokerlprice };
                    calc.inputTextDataArray[
0= sendArray[0];
                    calc.inputTextDataArray[
1= sendArray[1];
                    calc.inputTextDataArray[
2= sendArray[2];
                    calc.inputTextDataArray[
3= sendArray[3];
                    calc.inputTextDataArray[
4= sendArray[4];
                    
this.Close();
            }

        }


        
public   void  setDataGridReadOnly()
        
{
            
this.dataGridView1.ReadOnly = true;
        }
form1窗体中的主要事件代码为:
public   static   string [] inputTextDataArray  =   new   string []  """""""""" } ;
        
private  LinkDataBase link  =   new  LinkDataBase();
        
public  calc()
        
{
                            
            InitializeComponent();
            
this.comboBox2.Items.Add("");
        }


// -------将双击选择得到的用纸类型信息显示到窗体中--------
         private   void  setTextData()
        
{
            
this.comboBox2.IntegralHeight = false;//使组合框不调整大小以显示其所有项
            this.comboBox2.DroppedDown = false;//使组合框不显示其下拉部分
            this.comboBox2.Items[0= inputTextDataArray[0];
            
this.comboBox2.SelectedIndex = 0;

            
this.textBox20.Text = inputTextDataArray[1];
            
this.textBox18.Text = inputTextDataArray[2];
            
this.textBox10.Text = inputTextDataArray[3];
            
this.textBox2.Text = inputTextDataArray[4];
            
this.comboBox2.IntegralHeight = true;//恢复默认值
        }


        
// ----------创建窗体,供用户选择用纸类型----------
         private   void  cmb_Stoker_DropDown( object  sender, System.EventArgs e)
        
{
            make newFrm 
= new make();
            newFrm.setDataGridReadOnly();
            newFrm.ShowDialog();
            setTextData();
            SendKeys.Send(
"{Tab}");//向活动应用程序发送Tab键,跳到下一控件
        }


        
// --------将所选用纸信息的相关数据读入窗体---------
         private   void  cmb_StokerID_KeyUp( object  sender, System.Windows.Forms.KeyEventArgs e)
        
{
            
this.textBox20.Text = "";
            
this.textBox18.Text = "";
            
this.textBox10.Text = "";
            
this.textBox2.Text = "";
            
string strSearchWord = this.comboBox2.Text;
            
string sendSQL = "select papertypes,dprice,ke,xishu,lprice from system_paperprice where papertypes = '" + strSearchWord + "'";
            DataTable tempDataTable 
= this.link.SelectDataBase(sendSQL);
            
if (tempDataTable.Rows.Count > 0)
            
{
                inputTextDataArray[
0= tempDataTable.Rows[0][0].ToString().Trim();
                inputTextDataArray[
1= tempDataTable.Rows[0][1].ToString().Trim();
                inputTextDataArray[
2= tempDataTable.Rows[0][2].ToString().Trim();
                inputTextDataArray[
3= tempDataTable.Rows[0][3].ToString().Trim();
                inputTextDataArray[
4= tempDataTable.Rows[0][4].ToString().Trim();
                
this.setTextData();
                SendKeys.Send(
"{Tab}");//向活动应用程序发送Tab键,跳到下一控件
            }

        }

你可能感兴趣的:(双击dataGridView后的数据传递示例)