当前位置:编程学习 > C#/ASP.NET >>

关于DataGridView新增修改


如上图,我创建DataGridView对象后,在页面加载时会取数据库里相应数据。
然后,
假如新增行或修改行明细时,希望能即时保存到数据库里。
请问该怎么做呢? C# DataGridView --------------------编程问答-------------------- 在后面添加一个操作列,选择CommandFiled,然后把所有列都转成TemplateFiled. 一点编辑,后面什么都出来了。 --------------------编程问答-------------------- --------------------编程问答--------------------

  private void txinfo_CellClick(object sender, DataGridViewCellEventArgs e)         { 
            tel_ = txinfo.Rows[e.RowIndex].Cells["tel"].Value.ToString().Trim();             label3.Text = tel_;         }  
        private void txinfo_CellValueChanged(object sender, DataGridViewCellEventArgs e)         { 
            if (e.RowIndex > -1)             { 
                string tell = txinfo.Rows[e.RowIndex].Cells["tel"].FormattedValue.ToString().Trim();                 if (tel_ == tell)                 { 
                    string sqlStr = "update ywtx set province='" + txinfo.Rows[e.RowIndex].Cells["province"].FormattedValue.ToString() +                     "', city='" + txinfo.Rows[e.RowIndex].Cells["city"].FormattedValue.ToString() +                     "', name='" + txinfo.Rows[e.RowIndex].Cells["name"].FormattedValue.ToString() +                     "', email='" + txinfo.Rows[e.RowIndex].Cells["email"].FormattedValue.ToString() +                     "', address='" + txinfo.Rows[e.RowIndex].Cells["address"].FormattedValue.ToString() +                     "', need='" + txinfo.Rows[e.RowIndex].Cells["need"].FormattedValue.ToString() +                     "', price='" + txinfo.Rows[e.RowIndex].Cells["price"].FormattedValue.ToString() +                     "', gh='" + txinfo.Rows[e.RowIndex].Cells["gh"].FormattedValue.ToString() +                     "', time='" + txinfo.Rows[e.RowIndex].Cells["time"].FormattedValue.ToString() +                     "', bz='" + txinfo.Rows[e.RowIndex].Cells["bz"].FormattedValue.ToString() +                     "', zt='" + txinfo.Rows[e.RowIndex].Cells["zt"].FormattedValue.ToString() +                     "' where tel='" + tell + "'";                     conn.Open(); 
                    SqlCommand comm = new SqlCommand(sqlStr, conn);                     comm.ExecuteNonQuery();                     conn.Close();                 }                 else                 { 
                    string sqlStr = "select count(*) from ywtx where tel='" + tell + "' ";                     SqlCommand comm = new SqlCommand(sqlStr, conn);                     conn.Open(); 
                    int result = (int)comm.ExecuteScalar();                     conn.Close();                     if (result > 0)                     { 
                        MessageBox.Show("电话号码已存在!请修改!");                     }                     else                     {                         string 
sqlStr1 

"update 
ywtx 
set 
province='" 









补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,