读书人

如何在datagridview和数据绑定后在中

发布时间: 2012-09-01 09:33:02 作者: rapoo

怎么在datagridview和数据绑定后,在中间插入行,删除行
datagridview和access数据库已经绑定,我想在中间插入行,我就在网上找,写了个代码
DataTable dt = new DataTable();
dt = (DataTable)this.dataGridView1.DataSource;

for (int i = 0; i < input_unit * (input_comp + 1); i++)
{
DataRow row = dt.NewRow();
if ((i + 1) % (input_comp + 1) == 0)
dt.Rows.InsertAt(row, i);
}
this.dataGridView1.DataSource = dt;
但是数据库更新后,新增的行,在数据库中没有出现在想出现的位置,都被放到了后面。用同样的方法也不能实现删除功能。我想是否用oledbcommandbuilder类中的insert和delete方法,但是不知道怎么用,请各位高手指条明路。

[解决办法]
当然,,你数据库中又没有按你的方法排序
数据库中要有排序字段
[解决办法]
手动添加....你这样想要插入数据我试了下好象是不可以的
比如你有序号ID,自增长的话,又不能你自己写
最好的办法是,你每行数据后面再追加一行排序字段(自己写1,2,3,4~~~~)然后INSERT完在ORDER BY 排序字段. 当然,你必须同时为插入行以后的行排序字段累加1

然后再填充一次DT.

如果你还需要选择到原来的行....那么,你可以跳转行,具体请见
http://topic.csdn.net/u/20120613/10/2b9c371b-0794-46eb-94ab-cf4b8a0f842d.html?r=78857902
[解决办法]
写了一下,新手的关系,写的比较累赘,见笑

string conn = ConfigurationManager.ConnectionStrings["WindowsFormsApplication1.Properties.Settings.setting"].ToString();

SqlConnection theconnection = new SqlConnection(conn);

theconnection.Open();
try
{
foreach (DataGridViewRow row in this.dataGridView1.SelectedRows)
{
string A = row.Cells["paixu"].Value.ToString();
int B =int.Parse(A);
int c = B + 1;
int x = dataGridView1.CurrentRow.Index;
textBox6.Text = x.ToString();


string sql2 = string.Format("UPDATE test SET paixu = paixu + 1 WHERE paixu>='" + c + "'");
SqlCommand cmd1 = new SqlCommand(sql2, theconnection);
cmd1.ExecuteNonQuery();

string sql1 = string.Format("insert into test(id,A,B,C,D,paixu) values('" + this.textBox5.Text.Trim().ToString() + "','" + this.textBox1.Text.Trim().ToString() + "','" + this.textBox2.Text.Trim().ToString() + "','" + this.textBox3.Text.Trim().ToString() + "','" + this.textBox4.Text.Trim().ToString() + "','" + c + "')");
SqlCommand cmd = new SqlCommand(sql1, theconnection);

cmd.ExecuteNonQuery();


}
}


catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "提示");
}


finally
{



SqlCommand thecommand = new SqlCommand();
thecommand.Connection = theconnection;
thecommand.CommandText = "select * from TEST order by paixu";

DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(thecommand);
sda.Fill(dt);

dataGridView1.DataSource = dt;
int y = int.Parse(textBox6.Text);
dataGridView1.CurrentCell=dataGridView1.Rows[y].Cells[0];
paixu是真正的排序列
过程是这样的
选中行后,比选中行中的paixu值大的所有paixu值都+1,然后新增一条数据,排序就等于选中行中的paixu值+1
再数据填充,并order by paixu

在之前首先int x = dataGridView1.CurrentRow.Index;,找出行号,把他记录到TEXTBOX里(最简单),在finally后,再获取这个值,并作出跳转

------解决方案--------------------


删除选定行:
string conn = ConfigurationManager.ConnectionStrings["*******"].ToString();

SqlConnection theconnection = new SqlConnection(conn);

theconnection.Open();
{
if (dataGridView1.DataSource == null || dataGridView1.CurrentRow == null)
{
return;
}
else
{
if (this.dataGridView1.SelectedRows.Count > 0)
{
DialogResult dr = MessageBox.Show("确定删除选中的记录 ", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (dr == DialogResult.OK)
{
try
{

foreach (DataGridViewRow row in this.dataGridView1.SelectedRows)
{
string strName = row.Cells["ID"].Value.ToString();
string sql = string.Format("delete from 表 where ID= " + strName + "");

SqlCommand cmd = new SqlCommand(sql, theconnection);
cmd.ExecuteNonQuery();
cmd.Dispose();

}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "提示");
}
finally
{


SqlCommand thecommand = new SqlCommand();
thecommand.Connection = theconnection;
thecommand.CommandText = "select * from 表 order by 列";

DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(thecommand);
sda.Fill(dt);

dataGridView1.DataSource = dt;


}
}


else
{
return;
}
}
}
}

读书人网 >C#

热点推荐