Click here to Skip to main content
16,011,988 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datagridview in that edit and delete buttons there.I am loading
datagridview from the database table.And I click the datagridview Edit button respective data showing in the respective textboxes fine.and i click submit button data is saving database and showing datagridview.For this purpose used datagridview cellcontent click event.what i want is if i click id column header on datagridview data needs to sorted.How to do that.I am getting Argument out of Range Exception "Index out of range Exception".Plaese help me on this.

What I have tried:

C#
<blockquote class="quote"><div class="op">Quote:</div> 

    private void dgvwDtls_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            ChqMngrBsns.Masters.BankInfo bnkinfo = new ChqMngrBsns.Masters.BankInfo();

            bnkid = Convert.ToInt32(dgvwDtls.Rows[e.RowIndex].Cells["bankid"].Value);
            if (dgvwDtls.Columns[e.ColumnIndex].Name == "Edit" && e.RowIndex >= 0)
             {
                 txtBnknm.Text = dgvwDtls.Rows[e.RowIndex].Cells["bankname"].Value.ToString();


                 string CTS = dgvwDtls.Rows[e.RowIndex].Cells["Chequetype"].Value.ToString();


                 if (CTS == "True")
                 {
                     rbchqtype1.Checked = true;
                 
                 }

                 else if (CTS == "False")
                 {
                     rbchqtype2.Checked = true;
                 
                 }
                 
                 txtAcntno.Text = dgvwDtls.Rows[e.RowIndex].Cells["Accountno"].Value.ToString();
                 cmbAcntype.Text = dgvwDtls.Rows[e.RowIndex].Cells["Accounttype"].Value.ToString();
                 Isinsert = false;
            }
                else if (dgvwDtls.Columns[e.ColumnIndex].Name == "Delete" && e.RowIndex >= 0)
                {
                    bnkid = Convert.ToInt32(dgvwDtls.Rows[e.RowIndex].Cells["bankid"].Value);
                    DialogResult dr = MessageBox.Show("Are you sure to delete row?", "Confirmation", MessageBoxButtons.YesNo);
                    if (dr == DialogResult.Yes)
                    {
                        bnkinfo.DeleteData(bnkid);
                    }
                    else if (DialogResult == DialogResult.No)
                    {
                        MessageBox.Show("Nothing Deleted");
                    }

                    LoadDataGridView();
                
                
                }


             }</blockquote
Posted
Updated 19-May-17 3:00am
v2
Comments
RickZeeland 19-May-17 8:18am    
Can you also show the code where you set the datagridview.datasource ?
Member 13153537 19-May-17 8:20am    
This is the code for the loading data to datagridview

private void LoadDataGridView()
{
string connection = @"Data Source=CISBLR-01\SQL2008; Initial Catalog=ChequeManager;Integrated Security=True;";
SqlConnection con = new SqlConnection(connection);
SqlCommand cmd = new SqlCommand("SELECT bank_Id,row_number() over(order by [bank_Id]) as SlNo,[bank_Name],[bank_Accountno],[bank_Accountype],[bank_Chequetype] FROM [dbo].[bankdetails] order by SlNo", con);
try
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
dgvwDtls.DataSource = dt;
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Member 13153537 19-May-17 8:30am    
How to Handle that Exception in my case.Please give any reference.Thanks in advance.
RickZeeland 19-May-17 8:35am    
I think you need to check: e.RowIndex >= 0
Member 13153537 19-May-17 8:42am    
I know it is silly question.I'm fresher.Thanks for the help.

Check for:
e.RowIndex >= 0
You can use the .SortMode property for columns, see:
How to: Set the Sort Modes for Columns in the Windows Forms DataGridView Control[^]
Also see: sort datagridview on column header click[^]
 
Share this answer
 
v2
Comments
Member 13153537 19-May-17 8:27am    
If I select first column value and click column header it is working fine.If i directly click the column header i am getting argument out of range exception.Any way Thanks for the Help.
RickZeeland is right. I post this second solution with a slight correction from the code you showed:
C#
private void dgvwDtls_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
   if (e.RowIndex > -1) {
      ChqMngrBsns.Masters.BankInfo bnkinfo = new ChqMngrBsns.Masters.BankInfo();
      bnkid = Convert.ToInt32(dgvwDtls.Rows[e.RowIndex].Cells["bankid"].Value);
      if (dgvwDtls.Columns[e.ColumnIndex].Name == "Edit")
      {
         txtBnknm.Text = dgvwDtls.Rows[e.RowIndex].Cells["bankname"].Value.ToString();
         string CTS = dgvwDtls.Rows[e.RowIndex].Cells["Chequetype"].Value.ToString();
         if (CTS == "True")
         {
            rbchqtype1.Checked = true;
         }
         else if (CTS == "False")
         {
            rbchqtype2.Checked = true;
         }
         txtAcntno.Text = dgvwDtls.Rows[e.RowIndex].Cells["Accountno"].Value.ToString();
         cmbAcntype.Text = dgvwDtls.Rows[e.RowIndex].Cells["Accounttype"].Value.ToString();
         Isinsert = false;
      }
      else if (dgvwDtls.Columns[e.ColumnIndex].Name == "Delete")
      {
         bnkid = Convert.ToInt32(dgvwDtls.Rows[e.RowIndex].Cells["bankid"].Value);
         DialogResult dr = MessageBox.Show("Are you sure to delete row?", "Confirmation", MessageBoxButtons.YesNo);
         if (dr == DialogResult.Yes)
         {
            bnkinfo.DeleteData(bnkid);
         }
         else if (DialogResult == DialogResult.No)
         {
            MessageBox.Show("Nothing Deleted");
         }
         LoadDataGridView();
      }
   }
}

If you place the test of the RowIndex at the beginning, you get an early exit of the method. Strange that the CellContentClick event is fired when you click on a column header, though.
 
Share this answer
 
Comments
Member 13153537 19-May-17 9:07am    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900