导航

DataGridView 为TextBox初始化,单击事件

Posted on 2011-03-30 16:19  beeone  阅读(780)  评论(0编辑  收藏  举报
/*
* 窗体描述:显示“长春市六条主要江河八五年洪水情况表”信息;
* 功能: ;
* 方法:GetRiverString();//根据传参“河流名称”“数据库连接串”进行查询
* 作者: HeTao
* 日期: 2011-03-12, 星期六
* 版本:V1.0
*
* 修改人员:
* 修改日期:
* 修改内容:
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace YNRBSYS.RiversInfoQuery
{
public partial class uc_6JL85HS : UserControl
{

/// <summary>
///
/// </summary>
/// <param name="RiverName">河流名称</param>
/// <param name="connString">数据库连接串</param>

public void GetRiverString(string RiverName, string connString)
{
try
{
SqlConnection conn
= new SqlConnection(connString);

conn.Open();

string SqlStr = "SELECT * from CC_RIVER_HSXX "+
"WHERE (RIVERCODE ='" + RiverName + "')";

DataSet ds
= new DataSet();

SqlDataAdapter sda
= new SqlDataAdapter();

sda.SelectCommand
= new SqlCommand(SqlStr, conn);

sda.Fill(ds,
"info");

SqlCommand scmd
= new SqlCommand(SqlStr, conn);

SqlDataReader objSqlReader
= scmd.ExecuteReader();

while (objSqlReader.Read())
{

dataGridViewX1.DataSource
= ds.Tables[0];

}

}
catch (Exception ex)
{

MessageBox.Show(ex.Message);

}

}

private void setDataGridViewString()
{
if (dataGridViewX1.SelectedRows.Count > 0)
{
int i = dataGridViewX1.SelectedRows[0].Index;

txtHSNF.Text
= dataGridViewX1.Rows[i].Cells[1].Value.ToString();

txtHLMC.Text
= dataGridViewX1.Rows[i].Cells[2].Value.ToString();

txtXQ.Text
= dataGridViewX1.Rows[i].Cells[4].Value.ToString();

txtZDSW.Text
= dataGridViewX1.Rows[i].Cells[5].Value.ToString();

txtZM.Text
= dataGridViewX1.Rows[i].Cells[3].Value.ToString();

txtZDLL.Text
= dataGridViewX1.Rows[i].Cells[6].Value.ToString();

txtFSSJ.Text
= dataGridViewX1.Rows[i].Cells[7].Value.ToString();

txtDWZYQKXZ.Text
= dataGridViewX1.Rows[i].Cells[8].Value.ToString();

txtDWZYQKZRT.Text
= dataGridViewX1.Rows[i].Cells[9].Value.ToString();

txtDWZYQKHS.Text
= dataGridViewX1.Rows[i].Cells[10].Value.ToString();

txtDWZYQKRK.Text
= dataGridViewX1.Rows[i].Cells[11].Value.ToString();

txtDWZYQKFW.Text
= dataGridViewX1.Rows[i].Cells[12].Value.ToString();

txtDWZYQKGD.Text
= dataGridViewX1.Rows[i].Cells[13].Value.ToString();

txtBZ.Text
= dataGridViewX1.Rows[i].Cells[14].Value.ToString();
}
}
public uc_6JL85HS()
{

InitializeComponent();

}
//dataGridView 为textbox初始化值
private void uc_6JL85HS_Load(object sender, EventArgs e)
{

setDataGridViewString();

}

private void dataGridViewX1_CellClick(object sender, DataGridViewCellEventArgs e)
{

setDataGridViewString();

}
}
}