Click here to Skip to main content
15,031,937 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to send the first cell of datagridview's current row index as a paramater to my stored procedure. i wrote that code it works well
this.musteriTableAdapter.DeleteCustomer(isTakibiDataSet.Musteri, Convert.ToInt32(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString()));
                this.musteriTableAdapter.Fill(this.isTakibiDataSet.Musteri);


and when i try it with messageBox to ask the user if sure to delete the row. but it deletes always first row. here is my code
C#
if (DialogResult.OK == MessageBox.Show("Are you Sure to delete ?", "Delete ?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
     {
         this.musteriTableAdapter.DeleteCustomer(isTakibiDataSet.Musteri, Convert.ToInt32(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString()));
         this.musteriTableAdapter.Fill(this.isTakibiDataSet.Musteri);
     }
     else
         return;
 }


what is wrong with my code here ?
Posted
Comments
Orcun Iyigun 20-Oct-11 15:58pm
   
Have you debugged the "dataGridView1.CurrentRow.Index" value? is it the same value? Why not use selectedrow property instead?
Erdinc27 20-Oct-11 16:06pm
   
i tried SelectedRow but it doesnt have Cells[] property. now i tried something like
dataGridView1.CurrentRow.Cells[0].Value.ToString()
it shows the right value when i use it out of the DialogResult scope but if i use it with DialogResult it gives the value of first row.
wizardzz 20-Oct-11 16:11pm
   
SelectedRow has ItemArray, use that.

1 solution

It looks like you aren't sending the row index, but rather the converted to integer value of the first cell of the current row.

Convert.ToInt32(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString()));

Why are you doing that instead of just this:

dataGridView1.CurrentRow.Index


Update:

Go ahead and use SelectedRow instead, use
Convert.ToInt32(SelectedRow.ItemArray[0])
   
v2
Comments
Erdinc27 20-Oct-11 16:08pm
   
thanks for the reply. i dont need currentRow's index i need the currentRow's first Cell's value. because there i have firm_id so i need to send that id to my stored procedure as parameter. so i can delete the selected row
wizardzz 20-Oct-11 16:12pm
   
Understood, see my comment to your reply to Orcun.
Erdinc27 20-Oct-11 16:16pm
   
thanks for the help. i noticed the DİalogResult change the value of the cell so i tried something like that and it works now.
private void button1_Click(object sender, EventArgs e)
{
int theValue = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value.ToString());
if (DialogResult.OK == MessageBox.Show("Kaydı Silmek İstediğinizden Emin misiniz ?", "Kaydı Sil ?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
{

this.musteriTableAdapter.DeleteCustomer(isTakibiDataSet.Musteri, theValue);
this.musteriTableAdapter.Fill(this.isTakibiDataSet.Musteri);
}
else
return;
}
wizardzz 20-Oct-11 16:19pm
   
Interesting work around, I would definitely be sure to document why you had to do that in your code so others don't ever break it on accident. Glad you found a solution.

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