DataGridView

Asked By Esmail
02-Sep-10 07:37 AM
Earn up to 0 extra points for answering this tough question.
I have added a check box colum.
I want that If user selects a checkbox that row should be deleted by using a button control named Delete
Give me the code if possible
Thank You

  re: DataGridView

Reena Jain replied to Esmail
02-Sep-10 07:48 AM
hi,

just use the following code on delete button code

public string FindDt()
      {
        int j = 0, k = 0;
        string ChannelCd = "";
        DataTable dt = new DataTable();

        for (int i = 0; i < dgChannel.RowCount; i++)
        {
          if (dgChannel.Rows[i].Cells[0].Value == null)
          {
            j = j + 1;
          }
          else if ((bool)dgChannel.Rows[i].Cells[0].Value == true)
          {
            k = k + 1;
            ChannelCd = dgChannel.Rows[i].Cells[3].Value.ToString();
          }
          else if ((bool)dgChannel.Rows[i].Cells[0].Value == false)
          {
            j = j + 1;
          }
        }
        if (j == dgChannel.RowCount)
        {
          ClsCommon.Error = 8;
          FrmError obj = new FrmError();
          obj.ShowDialog();
          ChannelCd = "";
        }
        else if (k > 1)
        {
          ClsCommon.Error = 9;
          FrmError obj = new FrmError();
          obj.ShowDialog();
          ChannelCd = "";
        }

        return ChannelCd;
      }

here cell is the unique value of row.
in channelcd this unique value would be get and then provide this unique value in database to delete the row and again bind the datagridview with new data

hope this will help you

  re: DataGridView

Sagar P replied to Esmail
02-Sep-10 10:29 AM

If you have inserted check box as first cell then use this code, other wise give your checkbox cell index properlly;

for (i = 0; i <= dataGridView.Rows.Count - 1; i++)

{

  if (dataGridView.Rows[i].Cells[0].Value != null)

  {

    if ((bool) dataGridView.Rows[i].Cells[0].Value == true)

    {

      dataGridView.Rows.RemoveAt(i);

    }

  }

}

  re: DataGridView

Sreenivasulu Pitla replied to Esmail
02-Sep-10 11:38 AM
Try the following code for delete button click event.

     private void button1_Click(object sender, EventArgs e)
      {
        System.Collections.ArrayList list = new System.Collections.ArrayList();
        foreach (DataGridViewRow row in customersDatagrid.Rows)
        {
          // find the checked row and ad it to the list
          if (row.Index != -1 && row.Cells[2].Value != null && Convert.ToBoolean(row.Cells[2].Value.ToString()))
          {
            list.Add(row.Cells[0].Value);
          }
        }
        this.deleteRows(list); // delete the list of rows from database
        this.fillDataGrid(); // re-fill the datagrid
      }

Let me know if you need code for deleteRows() and fillDataGrid()

Sreenivasulu P
keep track of your daily expenses @ www.khurch.com
Create New Account