I am populating a Gridview using a form with the help of AJAX. I am using List to populate the GridView. I want to add a delete button to each row in GV and when delete button is clicked that row should be deleted from GridView.
I think i have to convert my list to DataTable and then Delete the ASP.Net GridView Row using CommandField and OnRowDeleting event.
Currently i have a scrap/undo button that just increments and decremts the counts.
Here is my aspx code:
Code
Code
PowerShell
.auto-style1 {
width: 120px;
}
.auto-style2 {
margin-bottom: 20;
}
.auto-style3 {
margin-left: 82;
}
.auto-style4 {
width: 217px;
}
'>
' runat="server" Text="Scrap" CommandName="Scrap" Visible='' />
' runat="server" Text="Undo" CommandName="Undo" Visible='' />Code
Code
Code
Here is my .cs code:
using System;Code
using System.Collections.Generic;Code
using System.Linq;Code
using System.Web;Code
using System.Web.UI;
using System.Web.UI.WebControls;Code
namespace Year4AbsenceC#
{
public partial class AbsenceRequestMonitor : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ButtonSearch_Click(object sender, EventArgs e)
{
DoAction();
}
public Boolean ScraptheRequest(int id, bool action)
{
bool success = false;
success = DBOperation.ScrapAbsencedate(id, action);
return success;
}
protected void GridViewViewAllRequests_RowCommand(object sender, GridViewCommandEventArgs e)
{
//Determine the RowIndex of the Row whose Button was clicked.
int id = Convert.ToInt32(e.CommandArgument);
//Get the value of column from the DataKeys using the RowIndex.
// int id = Convert.ToInt32(GridViewViewAllRequests.DataKeys[rowIndex].Values[0]);
if(e.CommandName == "Scrap")
{
ScraptheRequest(id, true);
}
else if(e.CommandName == "Undo")
{
ScraptheRequest(id, false);
}
DoAction();
UpdatePanelInfo.Update();
}
public void DoAction()
{
string firstName = null;
string lastName = null;
firstName = TextBoxFirstName.Text.ToString();
lastName = TextBoxLastName.Text.ToString();
double PdCount = 0;
double OtherCount = 0;
if (firstName != null && lastName != null)
{
//gets student data from the Student Form
List l_studentAbsenceInfo = DBOperation.getStudentAbsenceInfo(firstName, lastName);
AbsenceMonitorData studentAbsenceInfo = l_studentAbsenceInfo.FirstOrDefault();
PdCount += l_studentAbsenceInfo.Where(x => x.reason == "Personal Day").Where(y => y.isApproved == true).Where(z => z.scrap == false).Select(a => a.daysMissed).Sum();
OtherCount += l_studentAbsenceInfo.Where(x => x.reason != "Personal Day").Where(y => y.isApproved == true).Where(z => z.scrap == false).Select(a => a.daysMissed).Sum();
GridViewViewAllRequests.DataSource = l_studentAbsenceInfo;
GridViewViewAllRequests.DataBind();
if (l_studentAbsenceInfo.Count > 0)
{
LabelGetPersonal.Text = PdCount.ToString();
LabelGetOther.Text = OtherCount.ToString();
LabelgetTotaldays.Text = (PdCount + OtherCount).ToString();
LabelGetFirstName.Text = studentAbsenceInfo.firstName.ToString();
LabelGetLname.Text = studentAbsenceInfo.lastname.ToString();
LabelGetEmail.Text = studentAbsenceInfo.studentEmail.ToString();
PanelAbsenceInfo.Visible = true;
LabelNoRows.Visible = false;
}
else
{
LabelNoRows.Visible = true;
}
}
}
}
}
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted Nov 9, 2018, 11:15 AM EST
Using TemplateFields
http://geekswithblogs.net/dotNETvinz/archive/2009/06/10/adding-rows-in-gridview-with-edit-update-and-delete-functionality.aspx
Using BoundFields
http://geekswithblogs.net/dotNETvinz/archive/2009/02/22/gridview-insert-edit-update-and-delete--the-ado.net-way.aspx
gaurav vinayaka19 reputationPosted Nov 9, 2018, 11:20 AM EST
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted Nov 9, 2018, 11:57 AM EST
Laxmidhar SahooRank #383 · 6.6K reputationPosted Nov 9, 2018, 5:11 PM EST
gaurav vinayaka19 reputationPosted Nov 12, 2018, 8:34 AM EST
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted Nov 12, 2018, 9:32 AM EST
gaurav vinayaka19 reputationPosted Nov 12, 2018, 9:50 AM EST
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted Nov 12, 2018, 11:10 AM EST
gaurav vinayaka19 reputationPosted Nov 12, 2018, 11:16 AM EST
Laxmidhar SahooRank #383 · 6.6K reputationPosted Nov 12, 2018, 4:50 PM EST
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted Nov 13, 2018, 5:00 AM EST
Sreekanth ReddyRank #726 · 3.2K reputationPosted Nov 20, 2018, 11:44 PM EST