C# 如何在行编辑中从表中获取值

C# 如何在行编辑中从表中获取值,c#,C#,我有一个网格视图,其中包含列id,f\u name,l\u name和salary。我想从网格视图中得到它的值 我有这个代码,你可以看看我的意思: protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) bindgrideview(); } protected void bindgrideview() {

我有一个网格视图,其中包含列
id
f\u name
l\u name
salary
。我想从网格视图中得到它的值

我有这个代码,你可以看看我的意思:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)

            bindgrideview();
    }
    protected void bindgrideview()
    {
        SqlConnection strcon1 = new SqlConnection(strcon);
        strcon1.Open();
        string ADDStr = "SELECT ID,F_name , L_name , salary FROM Employee ";
        SqlCommand ADDCmd = new SqlCommand(ADDStr, strcon1);
        DataTable table = new DataTable();
        SqlDataAdapter adapter = new SqlDataAdapter(ADDCmd);
        adapter.Fill(table);

        GridView1.DataBind();

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id= // what must be write here ? 
        GridView1.EditIndex = e.NewEditIndex;
        bindgrideview();
        SqlConnection strcon1 = new SqlConnection(strcon);
        strcon1.Open();
        string ADDStr = "SELECT ID FROM Employee ";
        SqlCommand ADDCmd = new SqlCommand(ADDStr, strcon1);
        Reademp(id);
    }
    public void Reademp(int ID)
    {
        SqlConnection strcon1 = new SqlConnection(strcon);
        strcon1.Open();
        string ADDStr = "SELECT ID,F_name , L_name , salary , [department ID]  ,  [saudi_nationality] , [position ID]  , [role ID] FROM Employee ";
        SqlCommand ADDCmd = new SqlCommand(ADDStr, strcon1);
        DataTable table = new DataTable();
        SqlDataAdapter adapter = new SqlDataAdapter(ADDCmd);
        adapter.Fill(table);
        GridView1.DataSource = table;
        GridView1.DataBind();
        SqlDataReader myDataReader;
        myDataReader = ADDCmd.ExecuteReader();
        myDataReader.Read();

        string F_name = myDataReader["F_name"].ToString();
        string L_name = myDataReader["L_name"].ToString();
        string department_ID = myDataReader["[department ID]"].ToString();
        string saudi_nationality = myDataReader["saudi_nationality"].ToString();
        string position_ID = myDataReader["[position ID]"].ToString();
        string role_ID = myDataReader["[role ID]"].ToString();
        myDataReader.Close();

    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        string F_name = TextBox1.Text;
        string L_name = TextBox2.Text;
        string salary = TextBox3.Text;
        int status = 1;

        SqlConnection strcon1 = new SqlConnection(strcon);
        strcon1.Open();
        string ADDStr = "ADDEMPLOYEE";
        SqlCommand ADDCmd = new SqlCommand(ADDStr, strcon1);
        ADDCmd.CommandType = CommandType.StoredProcedure;
        ADDCmd.Parameters.AddWithValue("@F_name", F_name);
        ADDCmd.Parameters.AddWithValue("@L_name", L_name);
        ADDCmd.Parameters.AddWithValue("@salary", salary);
        ADDCmd.Parameters.AddWithValue("@status", status);

        ADDCmd.ExecuteNonQuery();
        bindgrideview();
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
    }

如果您将datatable与GridView关联,则可能会出现这种情况

protected void bindgrideview()
{
   .....
   adapter.Fill(table);
   GridView1.DataSource = table; // <- missing
   GridView1.DataBind();
   ....
}

您可以使用事件参数中的值来查找已编辑的行并从网格中读取数据:

grid.Rows[e.NewEditIndex].Cells[cell_index].Text
在您的情况下,可能是这样的:

int id = Int32.Parse(GridView1.Rows[e.NewEditIndex].Cells[0].Text);

您需要列id的值??是的,我这样写,但有一个错误:int index=GridView1.Rows[e.NewEditIndex].RowIndex;int id=Convert.ToInt32(GridView1.DataKeys[index].Value.ToString;从上面的代码中,调用bindGridView.ok时,GridView1与查询结果不关联,如果我这样写的话:int index=GridView1.Rows[e.NewEditIndex].RowIndex;int id=Convert.ToInt32(GridView1.DataKeys[index].Value.ToString;是否写入?如果只在db表中定义了键,则该方法也可以。
int id = Int32.Parse(GridView1.Rows[e.NewEditIndex].Cells[0].Text);