Grid view delete option

Posted by Bhasker under Regular Expressions on 8/28/2010 | Points: 10 | Views : 1978 | Status : [Member] | Replies : 3
dear all
how to delete the record from grid view delete link button.
pl reply me soon.




Responses

Posted by: Vuyiswamb on: 8/28/2010 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
First thing you need to do is that when you bind grid you must bind the datakey(Primary key) and hind it on the Databound event and when you delete and get the id of the record and run a sp that will delete the record based on the ID.



Thank you for posting at Dotnetfunda
[Administrator]

Bhasker, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bhasker on: 8/28/2010 [Member] Starter | Points: 25

Up
0
Down
can u send me sample code pl?

Bhasker, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 8/30/2010 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
Good Day

Lets say you have a gridview that is defined like this


<asp:GridView ID="grdViewResponsibilities" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSourceResponsibilities"
Font-Names="Verdana" Font-Size="10pt" OnSelectedIndexChanged="grdViewResponsibilities_SelectedIndexChanged"
PageSize="20" Width="100%" CellPadding="3" GridLines="Horizontal" Visible="False" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkbx" runat="server" />
</ItemTemplate>
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:BoundField DataField="User" HeaderText="User" SortExpression="User">
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Resource" HeaderText="Resource" SortExpression="Resource">
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type">
<ItemStyle Wrap="False" />
</asp:BoundField>
</Columns>
</asp:GridView>


The above grid has a Checkbox on the First Column and other bound Fields , and at the top you see there is

DataKeyNames="id"


This is the primary key of the table you are binding. now in order to delete the selected record or records that have been checked you have to

        BLL.BLL obj = new BLL.BLL();


List<int> Resource_ID = new List<int>(); //Stores the ID of the Resources

List<String> Userarr = new List<String>(); //Stored the Staff ID

String Results = null; //This will Display the Result returned by the Stored Procedure

//Here We check if there is atleast one that is selected and the if the menu is Owners it must do the Following

//Loop through out the grid
foreach (GridViewRow row in GridView2.Rows)
{
//Lets look for the ID's of the Checked Records
CheckBox chkbx = (CheckBox)row.FindControl("CheckBox1");


//Only Look for the Checked Records
if (chkbx != null && chkbx.Checked)
{
//Add the ID's of the Records that are checked on a list
Resource_ID.Add(Convert.ToInt32(GridView2.DataKeys[row.RowIndex].Value));
//Add the Usernames of the owners in a lisst
Userarr.Add(row.Cells[2].Text);

//For all the Resources that have been selected do the follwing
for (int i = 0; i < Resource_ID.Count; i++)
{
//Here we doble check again if there are contents in the list
if (Resource_ID.Count > 0)
{
//Because the Table that containts the Owner and the resource has no names but only id's
//we get the ID of the selected Staff and we will use it in the second function Responsibility_Owner_Remove
int Staff_ID = obj.Get_User_Id(Convert.ToString(Userarr[i]), Convert.ToString(Session["ActiveDatabase"]));

//Here we are Removing the Staff from the Resource
Results = obj.Responsibility_Owner_Remove(Staff_ID,Resource_ID[i], Convert.ToString(Session["ActiveDatabase"]));

}
else
{
lblStatus.Text = "The Selection is invalid";
}
}

}

}


Hope this example will help you

Thank you for posting at dotnetfunda

Vuyiswa Maseko


Thank you for posting at Dotnetfunda
[Administrator]

Bhasker, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response