如何从RowEditing中的表中获取值

时间:2013-03-13 10:06:38

标签: c#

我的网格视图包含idf_namel_namesalary列。我想从网格视图中获取它的值。

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

    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 = "";
    }

2 个答案:

答案 0 :(得分:2)

也许你将数据表与GridView相关联。

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

然后你可以用

读取当前行中第一个单元格的值
int id = Convert.ToInt32(GridView1.Rows[e.NewEditIndex].Cells[0].Text);

答案 1 :(得分:1)

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

grid.Rows[e.NewEditIndex].Cells[cell_index].Text

在你的情况下,它会是这样的:

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