DataGridView控件研究(一)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;

namespace DataGridView
{
public partial class Form1 : Form
{
DataSet ds = null;

public Form1()
{
InitializeComponent();
}

/// <summary>
/// 
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
string sql = "SELECT [CompanyName],[ContactName] FROM [Customers];";

BLL.SqlHelper sh = new BLL.SqlHelper();
ds = sh.Select(sql);

dataGridView1.DataSource = ds.Tables[0];

}

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{

}

private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
}

private void Insert_Click(object sender, EventArgs e)
{
int index = this.dataGridView1.SelectedRows[this.dataGridView1.SelectedRows.Count-1].Index;
dataGridView1.DataSource = null;
ds.Tables[0].Rows.InsertAt(ds.Tables[0].NewRow(), index);
dataGridView1.DataSource = ds.Tables[0];
dataGridView1.ClearSelection();
dataGridView1.Rows[index].Selected = true;
}

private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{

}

private void dataGridView1_DataSourceChanged(object sender, EventArgs e)
{

}

private void dataGridView1_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{

}

private void Delete_Click(object sender, EventArgs e)
{
int index = this.dataGridView1.SelectedRows[this.dataGridView1.SelectedRows.Count - 1].Index;
dataGridView1.DataSource = null;
ds.Tables[0].Rows.RemoveAt(index);
dataGridView1.DataSource = ds.Tables[0];


}

private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
//MessageBox.Show(dataGridView1.SelectedRows.Count.ToString());
MessageBox.Show(dataGridView1.SelectedRows[0].Index.ToString());
}
}
}


窗体加载时datagridview1控件绑定了一个datatable

由于绑定数据源的datagridview无法增加行

故在 Insert_Click 事件中 ,先将 datagridview1 的 DataSource 属性 设为null

在 ds.tables[0] 中增加行

然后将datagridview1 的datasource 重新设为 ds.tables[0]


问题来了, 将 DataSource 重新设为 ds.tables[0] 时 , 默认选中哪一行?

为此  ,我在 DataBindingComplete事件中 弹出了绑定完成后 datagridview默认选中的行


发现有这样的情况 : 第一次绑定(也就是Form_Load 里的绑定) 时, 弹出的 MessageBox  里显示的是0

这点应该是合情合理 , 这点微软的工程师的思维和咱大多数人一样, 数据绑定完了,咱让他默认选中第一行,而不是第10行或是第11行(话说回来,万一一共就3行呢)


但是,当我选中第一行(也就是index为0)

然后,点击 插入 按钮  , 引发这个 Insert_Click 事件 ,从而 重新绑定 DataSource 时

出奇的发现 弹出 的MessageBox 里显示的是1


而且,当我选中第8行(也就是index 为7)

然后,点击 插入 按钮  , 引发这个 Insert_Click 事件 ,从而 重新绑定 DataSource 时

出奇的发现 弹出 的MessageBox 里显示的是7

百思不得其解


MS的工程师用了什么方法,竟然让datagridview能够记住上一次绑定数据时默认选中的行

并且在重新绑定数据源的时候再次选中这一行,并且很明显

这种“记忆”能力绝不是通过 记忆 index 来完成的

如果datagridview 记录上次选中行的index ,那么我在table 里面原索引的位置增加一行

下次绑定的时候 datagridview 应该选中我新增加的行才对



另一个奇怪的现象是

如果 在Inset_click 事件中 ,将DataSource 改成另一张表

那么MessageBox 弹出的 就会是0 

将Insert_Click 改成如下所示


   

private void Insert_Click(object sender, EventArgs e)
{
int index = this.dataGridView1.SelectedRows[this.dataGridView1.SelectedRows.Count-1].Index;
dataGridView1.DataSource = null;
ds.Tables[0].Rows.InsertAt(ds.Tables[0].NewRow(), index);
string sql = "SELECT * FROM [Employees];";

BLL.SqlHelper sh = new BLL.SqlHelper();
ds1 = sh.Select(sql);

//dataGridView1.DataSource = ds.Tables[0];
dataGridView1.DataSource = ds1.Tables[0];
dataGridView1.ClearSelection();
dataGridView1.Rows[index].Selected = true;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值