范围太大了.若是用户,直接在上面编辑就可以更新 删除了.不用自己写代码.如果用代码更新的话,对着那些单元格一个一个写内容进
dgvModelInfo.Rows[e.RowIndex].Cells["pic1Path"].Value = "c:\\aa.jpg";你可以参考这行代码.

解决方案 »

  1.   

    这个东西网上太多了,,,www.codeproject.com..search by "gridview"
      

  2.   

    http://tech.163.com/special/000915SN/stvs2005.html
    从这里可以看视频(讲座)
      

  3.   

    <asp:CommandField ButtonType="Link" ShowEditButton="true" UpdateText="Apply "
                                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();
    }
    }
     呵呵,希望对你有帮助
      

  4.   

    更新:重新绑定下数据源或重新load一下
      

  5.   

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {        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());
      

  6.   

    System.Data.SqlClient.SqlDataAdapter da;
    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();
    }
    }
    }
      

  7.   

    LS,,你那是datagrid,,,DataGrid的例子,我也有.和GridView 不一样。
      

  8.   

    把他换成GridView就行了。
      

  9.   

    System.Data.SqlClient.SqlDataAdapter da;
    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();
    }
    }
    }
      

  10.   

    /// <summary>
    /// 更新数据集方法
    /// </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;
    }这个比较简单