deleting a row of a gridview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • meydanshahi
    New Member
    • May 2010
    • 5

    deleting a row of a gridview

    hi!
    how can i delete a selected row of a gridview? and would you please tell me how can i update a specific cell of a gridview.
  • rouse02
    New Member
    • Nov 2011
    • 2

    #2
    There is actually a template button for this, if you go to add columns in the gridview editor, you can add a delete button, its under command buttons. This will however, delete your entity from the database as well.
    Otherwise, I believe that you will have to do some quires that just don't show that row.

    Like
    Make a button field and make it a template column, under the click event,
    Button btn = new (Button)sender;
    GridViewRow item = (GridViewRow)bt n.Parent.Parent ;


    try
    {
    conn = new System.Data.Sql Client.SqlConne ction(sqlEventG ridSource.Conne ctionString.ToS tring());
    conn.Open();
    selectCommand = new System.Data.Sql Client.SqlComma nd("SELECT * FROM table WHERE primaryKeyId <> @id", conn);
    selectCommand.P arameters.Add(" id", System.Data.Sql DbType.value of primaryKey).Val ue = item.Cells[index of primary key of row (note, starts at 0)].Text;
    selectCommand.E xecuteScalar();
    }
    catch (Exception ex) { lblException.Te xt = "Error: " + ex.Message; }

    This will return the rows that do not match the id of whatever row you did not want.
    After that you should modify it to put it in a list and then set the gridView.Databo und to list...
    Like So

    List<Class of you attributes>bla = new List<>();

    for (int i = 1; i < SqlCommand("Sel ect Count(*) From table", conn); i++)
    {
    bla.Items.Add(S qlCommand("Sele ct * From table Where pID = " + i + " And pID <> " + value, conn));
    }
    gridview.Databo und = bla;

    Hope this helps.

    Comment

    • adriancs
      New Member
      • Apr 2011
      • 122

      #3
      Code:
      private void RemoveRow()
      {
          int rowIndex = this.dataGridView1.CurrentCellAddress.Y;
      
          if (rowIndex < 0)
              return;
      
          this.dataGridView1.Rows.RemoveAt(rowIndex);
      }

      Comment

      • adriancs
        New Member
        • Apr 2011
        • 122

        #4
        To make a datagridview editable:

        Code:
        private void MakeThisDataGridViewEditable()
        {
            this.dataGridView1.ReadOnly = !this.dataGridView1.ReadOnly;
        }

        Comment

        Working...