范围太大了.若是用户,直接在上面编辑就可以更新 删除了.不用自己写代码.如果用代码更新的话,对着那些单元格一个一个写内容进
dgvModelInfo.Rows[e.RowIndex].Cells["pic1Path"].Value = "c:\\aa.jpg";你可以参考这行代码.
dgvModelInfo.Rows[e.RowIndex].Cells["pic1Path"].Value = "c:\\aa.jpg";你可以参考这行代码.
解决方案 »
- 关于DragLeave的问题
- WPF里如何使ListBoxItem 选中时不变色?
- 如何让ListBox随着内容自动变化宽度和高度?
- 请问Winform,TextBox中实现类似VS写代码时的智能提示大概怎么做呢...
- 你的可用分比别人少吧!每天发帖即可得 30 可用分!
- 怎么让textbox 不可以编辑
- 请问怎么才能去掉DATAGRID空间的边框啊
- winform 动态创建checkBox控件
- 请问关于搜索的问题
- 100分,关于条码打印的问题,急!!!
- 请问谁有asp.net+sql2000的对文件(.doc,excel)导入导出的源代码?
- tcp通信程序中如何在主线程中停止一个进入死循环的的子线程
从这里可以看视频(讲座)
HeaderText="Change" CancelText="Cancel" EditText="Change"> protected void gvC_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Edit"))
{
gvC.EditIndex = TypeFormat.GetInt(e.CommandArgument);
BindCurrentVehiclePricing();
}
if (e.CommandName == "Cancel")
{
gvC.EditIndex = -1;
// BindgvC();
}
if (e.CommandName == "Update")
{
gvC.EditIndex = -1;
// BindCurrentVehiclePricing();
}
}
呵呵,希望对你有帮助
{ GridView1.EditIndex = -1;
BindData();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData(); }
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int index = e.RowIndex; Label i;
i = (Label)GridView1.Rows[index].Cells[0].FindControl("Label1"); int id = Int32.Parse(i.Text.ToString());
//int id = Convert.ToInt32(GridView1.Rows[index].Cells[0].Text);
TextBox name, password; name = (TextBox)GridView1.Rows[index].Cells[1].FindControl("TextBox2");
password = (TextBox)GridView1.Rows[index].Cells[2].FindControl("TextBox3");
//string name=((TextBox)GridView1.Rows[index].Cells[1].FindControl("TextBox2").ToString()); //string password = ((TextBox)GridView1.Rows[index].Cells[2].FindControl("TextBox3").ToString()); fill(id, name.Text.ToString(), password.Text.ToString());
GridView1.EditIndex = -1;
BindData(); } private void fill(int id,string name,string password)
{ string str = "select * from users";
SqlConnection mycon = new SqlConnection(ConfigurationManager.ConnectionStrings["mysql"].ConnectionString); mycon.Open(); SqlDataAdapter sda = new SqlDataAdapter(str, mycon);
SqlCommandBuilder sb = new SqlCommandBuilder(sda); DataSet ds = new DataSet(); try
{
sda.Fill(ds, "users");
ds.Tables["users"].DefaultView.Sort = "UserId";
int index = ds.Tables["users"].DefaultView.Find(id); ds.Tables["users"].Rows[index]["UserName"] = name;
ds.Tables["users"].Rows[index]["Password"] = password; int rows = sda.Update(ds, "users"); }
catch
{ }
finally
{
}
}System.FormatException: 输入字符串的格式不正确。行 114: int id = Int32.Parse(i.Text.ToString());
System.Data.SqlClient.SqlParameter par;
System.Data.DataTable dt;
data d=new data();
string sql;
public void load()
{
try
{
dt=d.select("select * from admin");
this.dataGrid1.DataSource=dt;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
load();
}private void button1_Click(object sender, System.EventArgs e)//添加
{
try
{
sql="insert into admin values(@name,@pwd)";
da=new System.Data.SqlClient.SqlDataAdapter();
da.InsertCommand=new System.Data.SqlClient.SqlCommand(sql,data.conn);par=da.InsertCommand.Parameters.Add("@name",System.Data.SqlDbType.Int);
par.SourceColumn="name";
par.SourceVersion=System.Data.DataRowVersion.Current;par=da.InsertCommand.Parameters.Add("@pwd",System.Data.SqlDbType.Int);
par.SourceColumn="pwd";
par.SourceVersion=System.Data.DataRowVersion.Current;da.Update(dt);
dt.Clear();
load();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}private void button2_Click(object sender, System.EventArgs e)//删除
{
try
{
sql="delete from admin where id=@id";
da=new System.Data.SqlClient.SqlDataAdapter();
da.DeleteCommand=new System.Data.SqlClient.SqlCommand(sql,data.conn);par=da.DeleteCommand.Parameters.Add("@id",System.Data.SqlDbType.Int);
par.SourceColumn="id";
par.SourceVersion=System.Data.DataRowVersion.Original;dt.Rows[this.dataGrid1.CurrentRowIndex].Delete();da.Update(dt);
dt.Clear();
load();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}private void button3_Click(object sender, System.EventArgs e)//修改
{
try
{
sql="update admin set name=@name,pwd=@pwd where id=@id";
da=new System.Data.SqlClient.SqlDataAdapter();
da.UpdateCommand=new System.Data.SqlClient.SqlCommand(sql,data.conn);par=da.UpdateCommand.Parameters.Add("@name",System.Data.SqlDbType.Int);
par.SourceColumn="name";
par.SourceVersion=System.Data.DataRowVersion.Current;par=da.UpdateCommand.Parameters.Add("@pwd",System.Data.SqlDbType.Int);
par.SourceColumn="pwd";
par.SourceVersion=System.Data.DataRowVersion.Current;par=da.UpdateCommand.Parameters.Add("@id",System.Data.SqlDbType.Int);
par.SourceColumn="id";
par.SourceVersion=System.Data.DataRowVersion.Original;da.Update(dt);
dt.Clear();
load();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}private void button4_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
System.Data.SqlClient.SqlParameter par;
System.Data.DataTable dt;
data d=new data();
string sql;
public void load()
{
try
{
dt=d.select("select * from admin");
this.dataGrid1.DataSource=dt;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
load();
}private void button1_Click(object sender, System.EventArgs e)//添加
{
try
{
sql="insert into admin values(@name,@pwd)";
da=new System.Data.SqlClient.SqlDataAdapter();
da.InsertCommand=new System.Data.SqlClient.SqlCommand(sql,data.conn);par=da.InsertCommand.Parameters.Add("@name",System.Data.SqlDbType.Int);
par.SourceColumn="name";
par.SourceVersion=System.Data.DataRowVersion.Current;par=da.InsertCommand.Parameters.Add("@pwd",System.Data.SqlDbType.Int);
par.SourceColumn="pwd";
par.SourceVersion=System.Data.DataRowVersion.Current;da.Update(dt);
dt.Clear();
load();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}private void button2_Click(object sender, System.EventArgs e)//删除
{
try
{
sql="delete from admin where id=@id";
da=new System.Data.SqlClient.SqlDataAdapter();
da.DeleteCommand=new System.Data.SqlClient.SqlCommand(sql,data.conn);par=da.DeleteCommand.Parameters.Add("@id",System.Data.SqlDbType.Int);
par.SourceColumn="id";
par.SourceVersion=System.Data.DataRowVersion.Original;dt.Rows[this.dataGrid1.CurrentRowIndex].Delete();da.Update(dt);
dt.Clear();
load();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}private void button3_Click(object sender, System.EventArgs e)//修改
{
try
{
sql="update admin set name=@name,pwd=@pwd where id=@id";
da=new System.Data.SqlClient.SqlDataAdapter();
da.UpdateCommand=new System.Data.SqlClient.SqlCommand(sql,data.conn);par=da.UpdateCommand.Parameters.Add("@name",System.Data.SqlDbType.Int);
par.SourceColumn="name";
par.SourceVersion=System.Data.DataRowVersion.Current;par=da.UpdateCommand.Parameters.Add("@pwd",System.Data.SqlDbType.Int);
par.SourceColumn="pwd";
par.SourceVersion=System.Data.DataRowVersion.Current;par=da.UpdateCommand.Parameters.Add("@id",System.Data.SqlDbType.Int);
par.SourceColumn="id";
par.SourceVersion=System.Data.DataRowVersion.Original;da.Update(dt);
dt.Clear();
load();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}private void button4_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
/// 更新数据集方法
/// </summary>
/// <param name="ds">要更新的数据集</param>
/// <param name="table">表名</param>
/// <returns>bool</returns>
public bool update(DataSet ds,string table)
{
bool f = false;
try
{
this.sqlda = new SqlDataAdapter("select * from "+table,sqlcon);
System.Data.SqlClient.SqlCommandBuilder sqlcmdbd = new SqlCommandBuilder(this.sqlda);
this.sqlda.Update(ds,table); ds.Tables[table].Clear();
this.sqlda = new SqlDataAdapter("select * from "+table,sqlcon);
sqlda.Fill(ds,table);
f = true;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
f=false;
}
return f;
} /// <summary>
/// 删除方法
/// </summary>
/// <param name="ds"></param>
/// <param name="tables"></param>
/// <param name="index"></param>
public bool delete(DataSet ds,string table,int index)
{
bool f = false;
try
{
this.sqlda=new SqlDataAdapter("select * from "+table,this.sqlcon);
SqlCommandBuilder sb=new SqlCommandBuilder(this.sqlda);
this.ds.Tables[table].Rows[index].Delete();
this.sqlda.Update(ds,table); ds.Tables[table].Clear();
this.sqlda = new SqlDataAdapter("select * from "+table,sqlcon);
sqlda.Fill(ds,table);
f= true;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
f=false;
}
return f;
}这个比较简单