日期:2014-05-18 浏览次数:21274 次
int r = 0;
string c = "";
DataSet ds = new DataSet();
private void button1_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
if (skey.Text.Trim() == "")
{
MessageBox.Show("请输入分类名称", "提示");
return;
}
string strConnect = Properties.Settings.Default.PopInfoConnectionString;
SqlConnection conConnection = new SqlConnection(strConnect);
conConnection.Open();
string cmd = " select classname as 产品分类名称,uptime as 最后更新日期 from [zmlb] where classname='" + skey.Text.Trim() + "'";
SqlDataAdapter ada = new SqlDataAdapter(cmd, conConnection);
ada.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
{
r = e.RowIndex; /*获得具有焦点的行*/
c = dataGridView1.Rows[r].Cells[1].Value.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
dataGridView1.Rows.RemoveAt(r);
}
private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
string strConnect = Properties.Settings.Default.PopInfoConnectionString;
SqlDataAdapter ada = new SqlDataAdapter();
SqlConnection conConnection = new SqlConnection(strConnect);
SqlCommand com = new SqlCommand("Delete From zmlb Where classname=@x", conConnection);
com.Parameters.Add("@x", SqlDbType.Char, 10);
com.Parameters[0].Value = c; /*对数据库进行操作的参数添加*/
ada.DeleteCommand = com;
ada.Update(ds);
}
public int UpdateByDataSet(DataSet ds,string strTblName,string strConnection)
{
try
{
SqlConnection conn = new SqlConnection(strConnection));
SqlDataAdapter myAdapter = new SqlDataAdapter();
SqlCommand myCommand = new SqlCommand("select * from "+strTblName),(SqlConnection)this.conn);
myAdapter.SelectCommand = myCommand;
SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);
myAdapter.Update(ds,strTblName);
return 0;
}
catch(BusinessException errBU)
{
throw errBU;
}
catch(Exception err)
{
throw new BusinessException(err);
}
}
private void button2_Click(object sender, EventArgs e)
{
dataGridView1.Rows.RemoveAt(r);
UpdateByDataSet(dataGridView1.DataSource as DataTable,"zmlb",Properties.Settings.Default.PopInfoConnectionString);
}
------解决方案--------------------
private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
//...
com.ExecuteNonQuery();//加一句执行删除的就可以了
ada.Update(ds);
}
------解决方案--------------------
请不要高估你的描述能力或者我的理解能力。
------解决方案--------------------
if (DataGridView.IsCurrentCellDirty) DataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
xx.Update(xx)