110,535
社区成员
发帖
与我相关
我的任务
分享
private void button1_Click(object sender, EventArgs e)
{
string connStr = "server=localhost;database=zilong;uid=root;pwd=4460072";
using (MySqlConnection conn = new MySqlConnection(connStr))
{
ds = new DataSet();
string sql = "select * from student ";
MySqlDataAdapter da = new MySqlDataAdapter(sql,conn);
da.Fill(ds,"stu");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "stu";
}
}
private void button2_Click(object sender, EventArgs e)
{
string s = dataGridView1.CurrentRow.Index.ToString();
label1.Text = s;
....
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private DataTable dt;
private void Form1_Load(object sender, EventArgs e)
{
string connStr = "server=localhost;database=zilong;uid=root;pwd=4460072";
using (MySqlConnection conn = new MySqlConnection(connStr))
{
dt = new DataTable();
string sql = "select * from student ";
MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
da.Fill(dt);
dataGridView1.DataSource = dt;
}
}
//上移
private void button2_Click(object sender, EventArgs e)
{
int rowIndex = dataGridView1.CurrentRow.Index;
if (rowIndex == 0)
{
MessageBox.Show("这已经是第一行了");
return;
}
List<string> list = new List<string>();
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
list.Add(dataGridView1.SelectedRows[0].Cells[i].Value.ToString()); //把当前选中行的数据存入list数组中
}
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
dataGridView1.Rows[rowIndex].Cells[j].Value = dataGridView1.Rows[rowIndex - 1].Cells[j].Value;
dataGridView1.Rows[rowIndex - 1].Cells[j].Value = list[j].ToString();
}
dataGridView1.Rows[rowIndex - 1].Selected = true;
dataGridView1.Rows[rowIndex].Selected = false;
}
}
/*DataGridView 实现行[Row]的上下移动,我这里用到了SelectedRows[0],而没用CurrentRow是有原因的
主要是这两段代码:
dataGridView1.Rows[rowIndex - 1].Selected = true;
dataGridView1.Rows[rowIndex].Selected = false;
这两行代码大家因该都能看懂,移上去的哪行选中状态,移下去的的取消选中状态.
如果我用dataGridView1.CurrentRow.Cell[0].Value 他取得的值仍然是rowIndex索引行的值
要使用SelectedRows[0] ,就必须设置这个属性:dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
实现原理:就是上下两行,把单元格中的值进行交换...呵呵表面上看去是向上,下移动了
不知道大家还有什么好的选中方法没...请多多指教
*/
private void Form3_Load(object sender, EventArgs e)
{
//........得到DataTable的代码省略....
dataGridView1.DataSource = dt;
}
private void button1_Click(object sender, EventArgs e) //向上移动
{
int rowIndex = dataGridView1.SelectedRows[0].Index; //得到当前选中行的索引
if(rowIndex == 0)
{
MessageBox.Show("已经是第一行了!");
return;
}
List<string> list = new List<string>();
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
list.Add(dataGridView1.SelectedRows[0].Cells[i].Value.ToString()); //把当前选中行的数据存入list数组中
}
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
dataGridView1.Rows[rowIndex].Cells[j].Value = dataGridView1.Rows[rowIndex - 1].Cells[j].Value;
dataGridView1.Rows[rowIndex - 1].Cells[j].Value = list[j].ToString();
}
dataGridView1.Rows[rowIndex - 1].Selected = true;
dataGridView1.Rows[rowIndex].Selected = false;
}
private void button2_Click(object sender, EventArgs e) //向下移动
{
int rowIndex = dataGridView1.SelectedRows[0].Index; //得到当前选中行的索引
if (rowIndex == dataGridView1.Rows.Count -1)
{
MessageBox.Show("已经是最后一行了!");
return;
}
List<string> list = new List<string>();
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
list.Add(dataGridView1.SelectedRows[0].Cells[i].Value.ToString()); //把当前选中行的数据存入list数组中
}
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
dataGridView1.Rows[rowIndex].Cells[j].Value = dataGridView1.Rows[rowIndex + 1].Cells[j].Value;
dataGridView1.Rows[rowIndex + 1].Cells[j].Value = list[j].ToString();
}
dataGridView1.Rows[rowIndex + 1].Selected = true;
dataGridView1.Rows[rowIndex].Selected = false;
}
//http://blog.csdn.net/wxm3630478/archive/2009/06/10/4256851.aspx
BindingSource bs = new BindingSource();
private void ProgramList_Load(object sender, EventArgs e)
{
SqlConnection con = DBCON.SqlConnettion();
string sql="select * from SystemUpdateHis";
SqlDataAdapter sdp = new SqlDataAdapter(sql, con);
con.Open();
DataSet ds = new DataSet();
sdp.Fill(ds, "SystemUpdateHis");
bs.DataSource = ds;
bs.DataMember = "SystemUpdateHis";
con.Close();
dataGridView1.DataSource = bs;
}
private void 下一笔_Click(object sender, EventArgs e)
{
bs.MoveNext();
}
private void 上一笔_Click(object sender, EventArgs e)
{
bs.MovePrevious();
}
//下移一行
private void button2_Click( object sender, EventArgs e )
{
DataGridViewRow dr = dataGridView1.CurrentRow;
if ( dr.Index < dataGridView1.Rows.Count - 1 )
{
int index = dr.Index;
dataGridView1.Rows.Remove( dr );
dataGridView1.Rows.Insert( index + 1, dr );
dr.Selected = true;
dataGridView1.CurrentCell = dr.Cells[0];
}
}
//上移一行
private void button3_Click( object sender, EventArgs e )
{
DataGridViewRow dr = dataGridView1.CurrentRow;
if ( dr.Index > 0 )
{
int index = dr.Index;
dataGridView1.Rows.Remove( dr );
dataGridView1.Rows.Insert( index - 1, dr );
dr.Selected = true;
dataGridView1.CurrentCell = dr.Cells[0];
}
}
//首行
private void button4_Click( object sender, EventArgs e )
{
if ( dataGridView1.CurrentRow.Index != 0 )
{
DataGridViewRow dr = dataGridView1.CurrentRow;
dataGridView1.Rows.Remove( dr );
dataGridView1.Rows.Insert(0, dr );
dr.Selected = true;
dataGridView1.CurrentCell = dr.Cells[0];
}
}
//末行
private void button5_Click( object sender, EventArgs e )
{
if ( dataGridView1.CurrentRow.Index <dataGridView1.Rows.Count-1 )
{
DataGridViewRow dr = dataGridView1.CurrentRow;
dataGridView1.Rows.Remove( dr );
dataGridView1.Rows.Insert( dataGridView1.Rows.Count , dr );
dr.Selected = true;
dataGridView1.CurrentCell = dr.Cells[0];
}
}