How to get the index of a particular row in gridview upon clicking on a particular command button

Problem statement: I want to implement some like this that whenever a user chooses or clicks on selected records and hit the a button called deleted selected, then it should delete all the selected records.

 if you have a working code for doing this task i would be highly gratefull..

The code i am doing is as follow:

protected void btnSelected1_Click(object sender, EventArgs e)

{

int counter, Id = 0;

ArrayList arr = new ArrayList();

for (counter = 0; counter < GVCustomers.Rows.Count; counter++)

{

CheckBox chkid;

chkid = (CheckBox)(GVCustomers.Rows[counter].FindControl("chkdelete"));

if (chkid.Checked == true)

{

string ID = GVCustomers.DataKeys[e.RowIndex].Value.ToString();    This line is incorrect i want to get the id's of the rows how do i do that?

//Label lblBuyingVehicleID = (Label)gvVehiclebuying.Rows[counter].FindControl("lblVehicleID");

//Id = int.Parse(lblBuyingVehicleID.Text.ToString());

arr.Add(Id);

}

}

if (counter == GVCustomers.Rows.Count)

{

//calling method for delete cars

DeletePamela(arr);

arr.Clear();

}

}

protected void DeletePamela(ArrayList arr)

{

ArrayList arr1 = new ArrayList();StringBuilder sb = new StringBuilder();

arr1 = arr;

if (arr1.Count > 0)

{

for (int i = 0; i < arr1.Count; i++)

{

if (i == 0)

{

sb.Append(arr1[i]);

}

else

{

sb.Append(
",");

sb.Append(arr1[i]);

}

}

}

sb.ToString();

SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["BugConnection"]);SqlCommand cmd = new SqlCommand();

con.Open();

cmd.CommandText = "usp_DeleteSpecificPamela";

SqlParameter p = new SqlParameter("@IDS", ID);

cmd.Parameters.Add(p);

cmd.CommandType =
CommandType.StoredProcedure;SqlDataAdapter da = new SqlDataAdapter(cmd);

cmd.Connection = con;

cmd.ExecuteNonQuery();

DataSet dsNew = new DataSet();

da.Fill(dsNew);

GVCustomers.DataSource = dsNew;

GVCustomers.DataBind();

con.Close();

GetDetailsFromDatabse();

lblMessage.Text =
"Vehicle deleted successfully.";

}

0
suprodeep
10/2/2008 3:55:23 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

3 Replies
601 Views

Similar Articles

[PageSpeed] 43

What do you do to set a row to be deleted, because you said the user can select multiple rows to be deleted.

Also, based on your comments, you don't need to know the row index, you just need to loop through the rows and delete the ones that were selected to be deleted.


Darmark

Mark as Answer, if this reply answers your post.
0
Darmark
10/2/2008 4:37:24 PM

do you have a link provided to delete each record or do you have checkboxes to check each of them that have to be deleted and then you hit a delete button or??? how do you do it?

0
renitdaniel
10/2/2008 5:25:05 PM

 You can use foreach loop for the selected rows in the GridView like,

foreach (GridViewRows gr in GridView.Row)

{

   bool rowChecked = (CheckBox)gr.FindControl("chkSelect").Checked;

   if(rowChecked == true)

    {

        (Perform the delete operation....... )

    }

}


Cheers,
Achutha Krishnan

~ No one can do everything, but everyone can do something ~
0
engineerachu
10/2/2008 5:31:42 PM
Reply: