In Gridview - get ID of row

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nguyenlh
    New Member
    • Mar 2007
    • 25

    In Gridview - get ID of row

    i make a gridview and i make a code :
    delete row in gridview
    Code:
     public bool Delete(int id, out string Msg) 
    {
    Msg = "";
    try
    {
    SqlServerDb sql = new SqlServerDb();// library connect
    sql.Connect();
     
    string cmdText = "Delete from tbl_Status where Id=@Id";
     
    SqlParameterList paramList = new SqlParameterList();
     
    paramList.Add("@Id", id, SqlDbType.SmallInt);
    sql.ExecuteNonQuery(cmdText, SqlCommandType.Text, paramList);
    sql.Disconnect();
    return true;
    }
    catch (Exception ex)
    {
    Msg = ex.Message;
    return false;
    }
    }
    this funcition use delete a row in database
    and i ask how to i invoke id of each row in Gridview
    to pass into this function to delete row
    example : I make a another function for click button
    Code:
     
    protected void btn_delete_Click(object sender, EventArgs e)
    { 
    TMLib.Business.AddStatus status = new AddStatus();// 
    // i need do
    if( status.Delete( ?, out message);
    i don't know pass this value id 
     
     
    }
    Last edited by DrBunchman; Jun 25 '08, 10:25 AM. Reason: Added code tags - Please use the # button
  • Saad Alam
    New Member
    • Oct 2007
    • 27

    #2
    First Set AutoGenerateDel eteButton property of gridview to true. Now you will have a link button at the end of each row in gridview. When this link button is clicked a rowdeleting event of gridview will be fired. In the row deleting event you will need to call your deletion function. In the DataKeyNames property give the name of your primary key column.
    Now in the rowdeleting event of gridview, you should write code similar to this:

    protected void GridView1_RowDe leting(object sender, GridViewDeleteE ventArgs e)
    {
    //Here you will call your deletion function
    // You can get the id by using this code
    //string id = GridView1.DataK eyNames.ToStrin g();
    }

    You can also keep the id in invisible column of gridview.

    Comment

    • nguyenlh
      New Member
      • Mar 2007
      • 25

      #3
      I can delete row
      but i can ask you
      I have a textbox and In girdview i make a linkbutton Edit and i want when i click Edit then -- the field appear on textbox ( textbox not into gridview) and i can edit the field and after update it( table only a field ) -

      Comment

      • athir13en
        New Member
        • Feb 2008
        • 2

        #4
        I cant get the id with that:
        Code:
        Dim ID As String = GridView1.DataKeyNames.ToString()..
        Can any one can help.. please?
        Last edited by DrBunchman; Jun 25 '08, 10:26 AM. Reason: Added code tags - Please use the # button

        Comment

        • kshnbd
          New Member
          • Jun 2008
          • 3

          #5
          Use this
          Code:
           
          string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
          Regards,

          Free Online Diary
          Last edited by DrBunchman; Jun 25 '08, 10:26 AM. Reason: Added code tags - Please use the # button

          Comment

          Working...