导航

dataGridView 单机提数据到TextBox

Posted on 2011-03-29 23:38  beeone  阅读(296)  评论(0编辑  收藏  举报
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace TestOne
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public void SetDateSet(string xing, string ConnStr)
{
string SqlStr = "SELECT * FROM T_UserName WHERE(Xing='"+xing+"')";
string connStr = ConnStr;
SqlConnection conn
= new SqlConnection(connStr);
conn.Open();
SqlCommand cmd
= conn.CreateCommand();
cmd.CommandText
= SqlStr;

SqlDataAdapter adp
= new SqlDataAdapter(cmd);
DataTable dt
= new DataTable();
adp.Fill(dt);
dataGridViewX1.DataSource
= dt;


}

private void Form1_Load(object sender, EventArgs e)
{

textBoxX1.Text
= dataGridViewX1.Rows[0].Cells[1].Value.ToString().Trim();
textBoxX2.Text
= dataGridViewX1.Rows[0].Cells[2].Value.ToString().Trim();

}


private void dataGridViewX1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridViewX1.Rows.Count > 0)
{
int i = dataGridViewX1.SelectedRows[0].Index;
textBoxX1.Text
= dataGridViewX1.Rows[i].Cells[1].Value.ToString().Trim();
textBoxX2.Text
= dataGridViewX1.Rows[i].Cells[2].Value.ToString().Trim();

}
}

}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace TestOne
{
public partial class Test : Form
{
public Test()
{
InitializeComponent();
}

private void buttonX1_Click(object sender, EventArgs e)
{
Form1 f
= new Form1();
f.SetDateSet(
"", "Data Source=.;Initial Catalog=TestDB;Integrated Security=True");
f.Show();


}



}

}

dataGridview 间隔着现实红格子

以下是引用片段:
if (this.dataGridView1.Rows.Count != 0)
  {
  
for (int i = 0; i < this.dataGridView1.Rows.Count; )
  {
  
this.dataGridView1.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.Pink;
  i
+= 2;
  }
  }