双击dataGridView后的数据传递示例

双击dataGridView后的数据传递示例

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

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

stringmysql="selectpapertypesas用纸类型,lenas长,widas宽,keas克重,xishuas系数,dpriceas吨价,lpriceas令价fromsystem_paperprice";
DataSetgrid
=newDataSet();
stringconnectionString=ConfigurationManager.ConnectionStrings["进销存管理系统.Properties.Settings.ld_ysglConnectionString"].ConnectionString;
SqlConnectionmyConnection
=newSqlConnection(connectionString);
myConnection.Open();

grid.Clear();

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

}



// 双击选择用纸类型信息并回送到调用的窗体-------------
private void dataGridView1_DoubleClick( object sender,System.EventArgse)
{
if(this.dataGridView1.ReadOnly==true)
{
intintCurrentRowNumber=this.dataGridView1.CurrentCell.RowIndex;
stringsendStokername,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=newstring[]{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 LinkDataBaselink = 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.EventArgse)
{
makenewFrm
=newmake();
newFrm.setDataGridReadOnly();
newFrm.ShowDialog();
setTextData();
SendKeys.Send(
"{Tab}");//向活动应用程序发送Tab键,跳到下一控件
}


// --------将所选用纸信息的相关数据读入窗体---------
private void cmb_StokerID_KeyUp( object sender,System.Windows.Forms.KeyEventArgse)
{
this.textBox20.Text="";
this.textBox18.Text="";
this.textBox10.Text="";
this.textBox2.Text="";
stringstrSearchWord=this.comboBox2.Text;
stringsendSQL="selectpapertypes,dprice,ke,xishu,lpricefromsystem_paperpricewherepapertypes='"+strSearchWord+"'";
DataTabletempDataTable
=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键,跳到下一控件
}

}

你可能感兴趣的:(mysql,windows,活动)