DataGridView数据源为空记录集时以及SelectionChanged事件的处理方法

xiaosong2008 2012-10-11 11:50:13
我在做的一个窗体,分为快捷工具栏、编辑区、数据表三个部分,编辑区由TextBox等控件组成,进行数据的新增和编辑操作,数据表由一个DataGridView控件组成,用来显示数据源中的数据。目前,单击工具栏中的“编辑”按钮,则编辑区中的TextBox控件中显示DataGridView控件中选中行的数据以供修改,单击“保存”按钮后,保存修改后的数据,大部分功能已经实现。
目前,还有以下问题有待解决:
一、当DataGridView数据源为空记录集时,即下面的iniDgvParts()事件中,ds1.Tables[0]中没有一行数据时,单击“编辑”时,会提示“未将对象引用设置到对象的实例”错误,代码中红色部分为提示错误的地方;
下为代码:
\\单击“编辑”按钮触发事件
private void tbEdit_Click(object sender, EventArgs e)
{
iniDgvParts();
if (dgvParts == null)
{
MessageBox.Show("数据源为空!");
return;
}
else
{
partId1 = dgvParts.CurrentRow.Cells[0].Value.ToString();
if (partId1 == "" || partId1 == null)
{
MessageBox.Show("请选择要编辑的数据行!");
return;
}
else
{
strSql1 = "select * from parts where partId="+ Convert.ToInt32(partId1)+"";
database.getSqlCmd(strSql1);
txtPartCode.Text = dgvParts.CurrentRow.Cells[1].Value.ToString();
txtPartName.Text = dgvParts.CurrentRow.Cells[2].Value.ToString();
rtxtRemark.Text = dgvParts.CurrentRow.Cells[6].Value.ToString();
}
}
tbSave.Enabled = true;
}

//DataGriVIew控件加载数据源,dgvParts为DataGridView控件
private void iniDgvParts()
{
strSql1 = "select a.partId as 序号,a.partCode as 代码,a.partName as 名称, b.styleCategoryName as 款式类型,a.maintainDate as 维护日期,a.maintainMan as 维护人,a.remark as 备注 from parts a join clothStyleCategory b on b.styleCategoryId=a.clothStyleCategoryId ";
DataSet ds1 = operateData.getDs(strSql1, "tb1");
dgvParts.DataSource = ds1.Tables[0];
dgvParts.Columns[0].Visible = false;
}
else
{
return;
}
}

二、我想实现单击DataGridView控件中的某行记录,即在编辑区中显示相应的数据,该如何实现?我用了DataGridView控件的SelectionChanged事件和CellClick事件,没有实现成功。
以上问题,请大家帮忙看一下!谢谢!
...全文
440 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Hauk 2012-10-12
  • 打赏
  • 举报
回复
1、

if (dgvParts == null)
{
MessageBox.Show("数据源为空!");
return;
}

这个改成if (.Rows.Count == 0)



2、datagridview的SelectionMode改成FullROwSelect就可以使用SelectionChanged做事了
xiaosong2008 2012-10-12
  • 打赏
  • 举报
回复
private void dgv1_SelectionChanged(object sender, EventArgs e)
{
if (dgv1.Rows.Count == 1)
{
MessageBox.Show("没有相关数据!");
return;
}
else
{
txtCode.Text = dgv1.CurrentRow.Cells[0].ToString();
txtName.Text = dgv1.CurrentRow.Cells[1].ToString();
txtRatio.Text = dgv1.CurrentRow.Cells[2].ToString();
rtxtRemark.Text = dgv1.CurrentRow.Cells[3].ToString();
}
}
应为:
private void dgv1_SelectionChanged(object sender, EventArgs e)
{
if (dgv1.Rows.Count == 1)
{
MessageBox.Show("没有相关数据!");
return;
}
else
{
txtCode.Text = dgv1.CurrentRow.Cells[1].ToString();
txtName.Text = dgv1.CurrentRow.Cells[2].ToString();
txtRatio.Text = dgv1.CurrentRow.Cells[3].ToString();
rtxtRemark.Text = dgv1.CurrentRow.Cells[4].ToString();
}
}
但这并非产生问题的原因!

请大家继续帮忙!谢谢!
xiaosong2008 2012-10-12
  • 打赏
  • 举报
回复
谢谢haukwong!
一、记录集为空时,dataGridView控件Rows.Count值却是1的,因此上面的代码改成了
if (dgvParts.Rows.Count == 1)
{
MessageBox.Show("数据源为空!");
return;
}
二、我把datagridview的SelectionMode改成FullRowSelect,执行SelectionChanged事件时,程序提示错误“未将对象引用设置到对象的实例”,下为SelectionChanged事件代码:
private void dgv1_SelectionChanged(object sender, EventArgs e)
{
if (dgv1.Rows.Count == 1)
{
MessageBox.Show("没有相关数据!");
return;
}
else
{
txtCode.Text = dgv1.CurrentRow.Cells[0].ToString();
txtName.Text = dgv1.CurrentRow.Cells[1].ToString();
txtRatio.Text = dgv1.CurrentRow.Cells[2].ToString();
rtxtRemark.Text = dgv1.CurrentRow.Cells[3].ToString();
}
}

用CellClick事件可以运行,代码如下:
private void dgv1_CellClick(object sender, DataGridViewCellEventArgs e)
{
//在dgv1控件的CellClick事件中实现单击某条数据显示详细信息
txtCode.Text = dgv1.SelectedCells[1].Value.ToString();
txtName.Text = dgv1.SelectedCells[2].Value.ToString();
txtRatio.Text = dgv1.SelectedCells[3].Value.ToString();
rtxtRemark.Text = dgv1.SelectedCells[4].Value.ToString();
}

但有一个问题,如何让DataGridView控件记载数据源后,第一行是否是自动被选定的?即打开该窗体时,编辑区显示DataGridView控件第一行数据中的值?
Hauk 2012-10-12
  • 打赏
  • 举报
回复


//获取当前单元格所在的行
dataGridView1.CurrentCell.OwningRow
dataGridView1.CurrentRow


最好在取值前判断一下dataGridView1.CurrentRow.IsNewRow

如果改了SelectionMode的话,最好把MultiSelect改成False。不能多选.
也可以用下面这句来区行
dataGridView1.SelectedRows[0]

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧