查询在datagridview的row_updating事件中触发? [英] Query to fire in a row_updating event of datagridview?

查看:53
本文介绍了查询在datagridview的row_updating事件中触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
     conn.Open();
     int idx = e.RowIndex;
     GridViewRow row1 = GridView1.Rows[idx];
     string s = row1.Cells[0].Text;
     TextBox name = (TextBox)row1.Cells[1].Controls[0];
     TextBox course = (TextBox)row1.Cells[2].Controls[0];
     string q ="update Student set Name=''" +name.Text+"'',Course=''" +course.Text+"''";
     SqlCommand cmd=new SqlCommand(q,conn);
     cmd.ExecuteNonQuery();
            
     GridView1.EditIndex = -1;           
     conn.Close();
}


当我更新特定行时,所有行都使用相同的名称进行更新.我知道我需要在查询中使用WHERE关键字,但是我无法弄清楚在where子句中要写什么?方案

首先,这对编写代码非常不利.在RowUpdate中进行SQL调用不是一种好的编程方法.

现在遇到您的问题:

hemant_chauhan写道:

我知道我需要在查询中使用WHERE关键字,但我无法弄清楚在where子句中要写什么


看起来您正在尝试更新学生课程.如果一个学生只能学习一门课程,则需要定义(或可能存在)与该学生相关联的主键.由于主键是唯一的,因此您可以在将其绑定到Gridview之前检索它们,并在更新名称和过程时提取它,然后在where子句中使用该键.

如果学生可以开设多门课程,那么您需要一个标准化的数据库,在该数据库中再次使用主键可以对其进行更新.


如果我们不想使用sql,那么我可以采用哪种方法更新数据库?


protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
     conn.Open();
     int idx = e.RowIndex;
     GridViewRow row1 = GridView1.Rows[idx];
     string s = row1.Cells[0].Text;
     TextBox name = (TextBox)row1.Cells[1].Controls[0];
     TextBox course = (TextBox)row1.Cells[2].Controls[0];
     string q ="update Student set Name=''" +name.Text+"'',Course=''" +course.Text+"''";
     SqlCommand cmd=new SqlCommand(q,conn);
     cmd.ExecuteNonQuery();
            
     GridView1.EditIndex = -1;           
     conn.Close();
}


when i update a particular row all the rows get updated with the same name.I know i need to have a WHERE keyword in my query but i cant figure out what to write in the where clause?

解决方案

First of all, this is a very bad of writing code. having SQL calls in RowUpdate is not a good programming method.

Now coming to your problem:

hemant_chauhan wrote:

I know i need to have a WHERE keyword in my query but i cant figure out what to write in the where clause


Looks like, you are trying to update course of a Student. If one student can have only one course, then, you need to define(or there muse be) a primary key associated with a student. Since primary Key''s are unique, you can retrieve those before binding it to Gridview and extract that while updating as you did for name and course, then use that key in your where clause.

If student can have multiple courses, then you need to have a little normalized Database where again using primary keys you can update it.


which method can i apply then if we dont want to use sql to update database???


这篇关于查询在datagridview的row_updating事件中触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆