DataGridView怎样实现添加、删除、上移、下移一行
<h1>场景</h1><p>在Winform中使用DataGridView实现添加一行、删除一行、上移一行、下移一行。</p>
<p>注:</p>
<p>博客主页: <br /><a href="https://blog.csdn.net/badao_liumang_qizhi">https://blog.csdn.net/badao_liumang_qizhi</a>
<br />关注公众号 <br />霸道的步伐猿 <br />获取编程干系电子书、教程推送与免费下载。 </p>
<h1>实现 </h1>
<h2>添加一行 </h2>
private void TaskViewEditHelper_OnAddStep(object sender, EventArgs e)
{
DataGridViewRow dr = new DataGridViewRow();
dr.CreateCells(this.dataGridView_Task_ViewEdit);
dr.Cells.Value = "公众号" + this.dataGridView_Task_ViewEdit.Rows.Count;
dr.Cells.Value = "霸道的步伐猿";
dr.Cells.Value = "大量编程教程与资源";
//this.dataGridView_Task_ViewEdit.Rows.Insert(0, dr); //添加的行作为第一行
this.dataGridView_Task_ViewEdit.Rows.Add(dr);//添加的行作为末了一行
}
<h2>效果</h2>
<p></p>
<p> </p>
<p> </p>
<h2> </h2>
<p><img/> </p>
<h2>删除一行 </h2>
private void TaskViewEditHelper_OnRemoveStep(object sender, EventArgs e)
{
if (this.dataGridView_Task_ViewEdit.SelectedRows == null || this.dataGridView_Task_ViewEdit.SelectedRows.Count == 0)
{
XtraMessageBox.Show("请先选择删除步,单击第一列以选中行");
}
else
{
if (XtraMessageBox.Show("确定要删除选中步吗?") == System.Windows.Forms.DialogResult.OK)
{
foreach (DataGridViewRow dr in this.dataGridView_Task_ViewEdit.SelectedRows)
{
if (dr.IsNewRow == false)
{
//假如不是已提交的行,默认情况下在添加一行数据成功后,DataGridView为新建一行作为新数据的插入位置
this.dataGridView_Task_ViewEdit.Rows.Remove(dr);
}
}
}
}
}
<p> </p>
<h2>效果 </h2>
<p> <img/></p>
<p> </p>
<p> </p>
<h2>上移一行 </h2>
private void TaskViewEditHelper_OnUpStep(object sender, EventArgs e)
{
if (this.dataGridView_Task_ViewEdit.SelectedRows == null || this.dataGridView_Task_ViewEdit.SelectedRows.Count == 0)
{
XtraMessageBox.Show("请先选择一行,单击第一列以选中行");
}
else
{
if (this.dataGridView_Task_ViewEdit.SelectedRows.Index <= 0)
{
XtraMessageBox.Show("此行已在顶端,不能再上移!");
}
else
{
//留意:这里好坏绑定命据情况的上移行
// 选择的行号
int selectedRowIndex = GetSelectedRowIndex(this.dataGridView_Task_ViewEdit);
if (selectedRowIndex >= 1)
{
// 拷贝选中的行
DataGridViewRow newRow = dataGridView_Task_ViewEdit.Rows;
// 删除选中的行
dataGridView_Task_ViewEdit.Rows.Remove(dataGridView_Task_ViewEdit.Rows);
// 将拷贝的行,插入到选中的上一行位置
dataGridView_Task_ViewEdit.Rows.Insert(selectedRowIndex - 1, newRow);
dataGridView_Task_ViewEdit.ClearSelection();
// 选中最初选中的行
dataGridView_Task_ViewEdit.Rows.Selected = true;
}
}
}
}
<p> </p>
<p>注: </p>
<p>这里是没绑定命据源情况下的上移一行,添加的一行时通过是上面新增的方法实现的。 </p>
<p>此时dataGridView的dataSource是为空的。 </p>
<p>此中用到获取选中行的方法: </p>
private int GetSelectedRowIndex(DataGridView dgv)
{
if (dgv.Rows.Count == 0)
{
return 0;
}
foreach (DataGridViewRow row in dgv.Rows)
{
if (row.Selected)
{
return row.Index;
}
}
return 0;
}
<h2>效果</h2>
<p></p>
<p> </p>
<p> </p>
<h2> </h2>
<p><img/></p>
<h2>下移一行</h2>
private void TaskViewEditHelper_OnDownStep(object sender, EventArgs e)
{
if (this.dataGridView_Task_ViewEdit.SelectedRows == null || this.dataGridView_Task_ViewEdit.SelectedRows.Count == 0)
{
XtraMessageBox.Show("请先选择一行,单击第一列以选中行");
}
else
{
if (this.dataGridView_Task_ViewEdit.SelectedRows.Index >= this.dataGridView_Task_ViewEdit.Rows.Count - 1)
{
XtraMessageBox.Show("此行已在底端,不能再下移!");
}
else
{
int selectedRowIndex = GetSelectedRowIndex(this.dataGridView_Task_ViewEdit);
if (selectedRowIndex < dataGridView_Task_ViewEdit.Rows.Count - 1)
{
// 拷贝选中的行
DataGridViewRow newRow = dataGridView_Task_ViewEdit.Rows;
// 删除选中的行
dataGridView_Task_ViewEdit.Rows.Remove(dataGridView_Task_ViewEdit.Rows);
// 将拷贝的行,插入到选中的下一行位置
dataGridView_Task_ViewEdit.Rows.Insert(selectedRowIndex + 1, newRow);
dataGridView_Task_ViewEdit.ClearSelection();
// 选中最初选中的行
dataGridView_Task_ViewEdit.Rows.Selected = true;
}
}
}
}
<p> </p>
<h2>效果</h2>
<p></p>
<p> </p>
<p> </p>
<h2> </h2>
<p><img/></p> [天津时时彩]https://1680380.com/view/shishicai_tj/ssc_index.html[幸运飞艇]https://www.1680380.com/view/xingyft/pk10kai.html
[超级大乐透]https://1680380.com/view/cjdlt/index.html
[十一运夺金]https://1680380.com/view/shiyix5_sd/index.html[天津时时彩]https://1680380.com/view/shishicai_tj/ssc_index.html[幸运飞艇]https://www.1680380.com/view/xingyft/pk10kai.html
页:
[1]