Monday, July 11, 2011

GridView-Manually Add/Edit/Delete/Sort/Page of GridView without using data source control

Many a times while working with Gridview we want to work with the Add, edit, delete, paging and sorting functionality without using any datasource control.

Gridview is flexible enough to perform these tasks without the use of any datasource control.

<asp:GridView ID="GridView1" Runat="server" AutoGenerateColumns="False"
AllowPaging="True" AllowSorting="True" CellPadding="4"

EnableModelValidation="True" ForeColor="#333333" GridLines="None"
onrowediting="GridView1_RowEditing"

onrowupdating="GridView1_RowUpdating"
onrowcancelingedit="GridView1_RowCancelingEdit" onpageindexchanging="GridView1_PageIndexChanging"

onsorting="GridView1_Sorting" PageSize="10">

<AlternatingRowStyle BackColor="White" ForeColor="#284775" />

<Columns>



<asp:CommandField ShowEditButton="True" />



<asp:BoundField HeaderText="ID" DataField="ID" SortExpression="ID" ReadOnly="true" />

<asp:BoundField HeaderText="Start Date" DataField="StartDate" SortExpression="StartDate" dataformatstring="{0:M-dd-yyyy}" >
<itemstyle Width="100px" >
</itemstyle>
</asp:BoundField>



<asp:BoundField HeaderText="End Date" DataField="EndDate" SortExpression="EndDate">
<itemstyle Width="100px">

</itemstyle>
</asp:BoundField>



<asp:TemplateField HeaderText="Comments">

<ItemTemplate>

<a href="javascript:openPopup('OrganizationDetails.aspx?id=<%# Eval("ID") %>')"><%# Eval("Comments")%></a>

</ItemTemplate>

</asp:TemplateField>

<asp:BoundField HeaderText="Dt Posted" DataField="dtPosted" SortExpression="dtPosted" ReadOnly="true" >
<itemstyle Width="100px">

</itemstyle>
</asp:BoundField>





<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
OnClientClick='return confirm("Are you sure you want to delete this entry?");'

Text="Delete" />

</ItemTemplate>

</asp:TemplateField>



</Columns>

<EditRowStyle BackColor="#999999" />

<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />

<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />

<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />

<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />

<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>



public partial class PhysicianAccount_PhysicianAvaliability : System.Web.UI.Page

{
protected void Page_Load(object sender, EventArgs e)

{

try

{
if (!IsPostBack)

{

// Validate initially
if (Session["ResourceID"] == null)

{
Response.Redirect("../Default.aspx");

}
return clsResourceAvailabilityComponent.GetResourceSlotTypes();

BindGridView();

}

}
catch (Exception ex)

{

Response.Write(ex.ToString());

}

}

#region Bind GridView
private void BindGridView()

{

try

{
if (ViewState["GridViewData"] == null)

{
ViewState["GridViewData"] = clsResourceAvailabilityComponent.GetResourceAvailabilitDT();

ViewState["GridViewPageIndex"] = 0;ViewState[
"GridViewSortExpression"] = "ID";ViewState["GridViewDirection"] = "DESC";

}


DataView objDataView = new DataView((DataTable)ViewState["GridViewData"]);objDataView.Sort = ViewState["GridViewSortExpression"] + " " + ViewState["GridViewDirection"];

GridView1.PageSize = 2;
GridView1.PageIndex = (int)ViewState["GridViewPageIndex"];

GridView1.DataSource = objDataView;

GridView1.DataBind();

}
catch (Exception ex)

{
throw (ex);

}

}

#endregion

//public static System.Data.DataSet GetPhysicianSlotTypes()

//{



//}




protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

{
try

{

GridView1.EditIndex = e.NewEditIndex; // turn to edit mode

BindGridView(); // Rebind GridView to show the data in edit mode

}
catch (Exception ex)

{

Response.Write(ex.ToString());

}

}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

{

try

{

GridView1.EditIndex = -1; //swicth back to default mode

BindGridView(); // Rebind GridView to show the data in default mode

}
catch (Exception ex)

{

Response.Write(ex.ToString());

}

}


protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

{

try

{
const int cID_CELL_NO = 1;

const int cSTARTDATE_CELL_NO = 3;
const int cENDDATE_CELL_NO = 4;

//const int cMON_CELL_NO = 5;

//const int cTUE_CELL_NO = 6;

//const int cWED_CELL_NO = 7;

//const int cTHU_CELL_NO = 8;

//const int cFRI_CELL_NO = 9;

//const int cSAT_CELL_NO = 10;

//const int cSUN_CELL_NO = 11;





string sID = GridView1.Rows[e.RowIndex].Cells[cID_CELL_NO].Text; //ID

string sStartDate = ((TextBox)GridView1.Rows[e.RowIndex].Cells[cSTARTDATE_CELL_NO].Controls[0]).Text; //start Date

string sEndDate = ((TextBox)GridView1.Rows[e.RowIndex].Cells[cENDDATE_CELL_NO].Controls[0]).Text; //start Date


DateTime dtStartDate = clsConstants.NullDate;
DateTime dtEnddDate = clsConstants.NullDate;
bool bHasEndDate = false;if (clsCommon.IsDate(sStartDate))

{
dtStartDate = Convert.ToDateTime(sStartDate);

}

else

{
WebMsgBox.Show("Please enter a valid start date.");return;

}
if (clsCommon.IsDate(sEndDate))

{

dtEnddDate = Convert.ToDateTime(sEndDate);bHasEndDate =
true;if (DateTime.Compare(dtStartDate, dtEnddDate) > 0)

{

WebMsgBox.Show("End Date should be > Start Date.");
return;

}

}

else

{
if (String.IsNullOrEmpty(sEndDate) == false)

{
WebMsgBox.Show("Please enter a valid end date.");return;

}

}
GridViewRow objGrodViewRow = GridView1.Rows[e.RowIndex];

DropDownList objDDLMon = (DropDownList)objGrodViewRow.FindControl("ddlMon");
string sMon = objDDLMon.SelectedValue;

DropDownList objDDLTue = (DropDownList)objGrodViewRow.FindControl("ddlTue");
string sTue = objDDLTue.SelectedValue;

DropDownList objDDLWed = (DropDownList)objGrodViewRow.FindControl("ddlWed");
string sWed = objDDLWed.SelectedValue;

DropDownList objDDLThu = (DropDownList)objGrodViewRow.FindControl("ddlThu");
string sThu = objDDLThu.SelectedValue;

DropDownList objDDLFri = (DropDownList)objGrodViewRow.FindControl("ddlFri");
string sFri = objDDLFri.SelectedValue;

DropDownList objDDLSat = (DropDownList)objGrodViewRow.FindControl("ddlSat");
string sSat = objDDLSat.SelectedValue;

DropDownList objDDLSun = (DropDownList)objGrodViewRow.FindControl("ddlSun");
string sSun = objDDLSun.SelectedValue;

string sResourceSlotTypesXML = clsResourceAvailabilityComponent.GetResourceSlotTypesXML();
int iMonID = clsResourceAvailabilityComponent.GetResourceSlotIDFromName(sResourceSlotTypesXML, sMon);

int iTueID = clsResourceAvailabilityComponent.GetResourceSlotIDFromName(sResourceSlotTypesXML, sTue);
int iWedID = clsResourceAvailabilityComponent.GetResourceSlotIDFromName(sResourceSlotTypesXML, sWed);

int iThuID = clsResourceAvailabilityComponent.GetResourceSlotIDFromName(sResourceSlotTypesXML, sThu);
int iFriID = clsResourceAvailabilityComponent.GetResourceSlotIDFromName(sResourceSlotTypesXML, sFri);

int iSatID = clsResourceAvailabilityComponent.GetResourceSlotIDFromName(sResourceSlotTypesXML, sSat);
int iSunID = clsResourceAvailabilityComponent.GetResourceSlotIDFromName(sResourceSlotTypesXML, sSun);

clsResourceAvailabilityComponent.UpdateResourceAvailability(Convert.ToInt64(Session["ResourceID"]), sStartDate, bHasEndDate, sEndDate, iMonID, iTueID, iWedID, iThuID, iFriID, iSatID, iSunID, 0,
"", Convert.ToInt32(sID));ViewState["GridViewData"] = null;

GridView1.EditIndex = -1;

BindGridView();

}
catch (Exception ex)

{

Response.Write(ex.ToString());

}

}




protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)

{

try

{
string sortExpression = e.SortExpression;ViewState["GridViewSortExpression"] = sortExpression;


if (Convert.ToString(ViewState["GridViewDirection"]) =="ASC")

{
ViewState["GridViewDirection"] = "DESC";

BindGridView();

}

else

{
ViewState["GridViewDirection"] = "ASC";

BindGridView();

}

}
catch (Exception ex)

{

Response.Write(ex.ToString());

}



}


protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)

{
try

{
ViewState["GridViewPageIndex"] = e.NewPageIndex;

BindGridView();

}
catch (Exception ex)

{

Response.Write(ex.ToString());

}

}

}

No comments:

Post a Comment

If you’re using Visual Studio to build your .NET MAUI app on a Mac, locating the IPA (iOS App Package) file can be a bit tricky

  If you’re using   Visual Studio   to build your   .NET MAUI   app on a   Mac , locating the   IPA (iOS App Package)   file can be a bit tr...