Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Tuesday, 14 December 2010

Changing Column HeaderText of GridView Dynamically in the RunTime (C#)

I hope the following codes could help you. In the side of sample.aspx file:

<asp:GridView ID="GridView1" runat="server"  OnRowCreated = "GridView1_RowCreated"> </asp:GridView>

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cell[1].Text = "id";
e.Row.Cells[2].Text = "Employee ID";
//You can skip if you made visible="false" for the cell number 3.
e.Row.Cells[4].Text = "KPI Description";
}
}
In the other way, you can try applying looping in this function called GridView1_RowCreated.

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 0; i < e.Row.Cells.Count;i++)
{
if (e.Row.Cells[i].Text == “CORP”)
e.Row.Cells[i].Text = “WinCorp”;
if (e.Row.Cells[i].Text == “PTNR”)
e.Row.Cells[i].Text = “WinPtnr”;
if (e.Row.Cells[i].Text == “SCORP”)
e.Row.Cells[i].Text = “WinScorp”;
if (e.Row.Cells[i].Text == “FIDUC”)
e.Row.Cells[i].Text = “WinFiduc”;
if (e.Row.Cells[i].Text == “WINTAX”)
e.Row.Cells[i].Text = “WinTax”;
if (e.Row.Cells[i].Text == “WINPAY”)
e.Row.Cells[i].Text = “WinPay”;
if (e.Row.Cells[i].Text == “DEPR”)
e.Row.Cells[i].Text = “WinDepr”;
if (e.Row.Cells[i].Text == “CORP”)
e.Row.Cells[i].Text = “WinCorp”;
}
}
 
Reference: http://www.pederborg.dk/2007/01/25/changing-gridview-column-headertext-at-runtime/

Adding Drop Downl List & Textbox Pager Style inside the GridView of ASP.net (C#)

In the Grid View of aspx page, you should add the following codes:

<asp:GridView ID="GridView_AA" runat="server" 
DataKeyNames="ID,PERIOD_NAME,status" AllowPaging = "true" 
ondatabound="GridView_AA_DataBound" PageSize = "5" >
<%--Pager--%>
<PagerStyle forecolor="Blue" backcolor="LightBlue"/><PagerTemplate>
<table>
<tr>
<td style="width:150px">
<asp:label id="MessageLabel"
forecolor="white"
text="Select a page:"
runat="server"/>
<asp:dropdownlist id="PageDropDownList"
autopostback="true"
onselectedindexchanged="PageDropDownList_SelectedIndexChanged"
runat="server" Width="36px" />
</td>
<td style="width:540px; text-align:center;">
<asp:label id="CurrentPageLabel" forecolor="white" runat="server"/>
</td>
<%--Go Command--%>
<td style="width:200px; height:26px; text-align:right; float:right;">
Page : <asp:TextBox id="txtGo" runat="server" Width="30px" />
<asp:RangeValidator ID="rvtxtGo" runat="server"
ControlToValidate= "txtGo" Type="Integer"
MaximumValue="100" MinimumValue="1" ></asp:RangeValidator>
<asp:Button id="btnGo" Text="Go" runat="server" 
OnClick="btnGo_Click" Width="30px" />
</td>
<%--Go Command--%>
</tr>
</table>
</PagerTemplate>
<%--Pager--%>
</asp:GridView>
protected void GridView_AA_DataBound(Object sender, EventArgs e)
{
GridViewRow pagerRow = GridView_AA.BottomPagerRow;
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
if (pageList != null)
{
for (int i = 0; i < GridView_AA.PageCount; i++)
{
int pageNumber = i + 1;
ListItem item = new ListItem(pageNumber.ToString());
if (i == GridView_AA.PageIndex)
{
item.Selected = true;
}
pageList.Items.Add(item);
}
}
if (pageLabel != null)
{
int currentPage = GridView_AA.PageIndex + 1;
pageLabel.Text = "░ Page " + currentPage.ToString() +
" of " + GridView_AA.PageCount.ToString() + " ░";
}
}
protected void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e)
{
GridViewRow pagerRow = GridView_AA.BottomPagerRow;
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
try
{
SqlConnection sqlConn = new SqlConnection(connStr);
string sql = "SELECT * FROM ANNUAL_ASSESSMENT";
sqlConn.Open();
SqlDataAdapter da = new SqlDataAdapter();
da = new SqlDataAdapter(sql, sqlConn);
DataSet ds = new DataSet();
da.Fill(ds, "PAGING_ANNUAL_ASSESSMENT");
foreach (DataRow row in ds.Tables[0].Rows)
{
row[2] = Config.StrYYYYMMDDtoDate(row[2].ToString());
row[3] = Config.StrYYYYMMDDtoDate(row[3].ToString());
}
ds.AcceptChanges();
GridView_AA.DataSource = ds.Tables["PAGING_ANNUAL_ASSESSMENT"];
GridView_AA.PageIndex = Convert.ToInt32(pageList.SelectedValue) - 1;
GridView_AA.DataBind();
sqlConn.Close();
ImageSettingGridview();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
protected void btnGo_Click(object sender, EventArgs e)
{
GridViewRow pagerRow = GridView_AA.BottomPagerRow;
TextBox pageList = (TextBox)pagerRow.Cells[0].FindControl("txtGo");
try
{
SqlConnection sqlConn = new SqlConnection(connStr);
string sql = "SELECT * FROM ANNUAL_ASSESSMENT";
sqlConn.Open();
SqlDataAdapter da = new SqlDataAdapter();
da = new SqlDataAdapter(sql, sqlConn);
DataSet ds = new DataSet();
da.Fill(ds, "PAGING_ANNUAL_ASSESSMENT");
foreach (DataRow row in ds.Tables[0].Rows)
{
row[2] = Config.StrYYYYMMDDtoDate(row[2].ToString());
row[3] = Config.StrYYYYMMDDtoDate(row[3].ToString());
}
ds.AcceptChanges();
GridView_AA.DataSource = ds.Tables["PAGING_ANNUAL_ASSESSMENT"];
GridView_AA.PageIndex = Convert.ToInt32(pageList.Text) - 1;
GridView_AA.DataBind();
sqlConn.Close();
ImageSettingGridview();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}

I hope you can try by yourself according to the above example.

Reference: http://www.aspnettutorials.com/tutorials/controls/gvpagertemplate-csharp.aspx

Sorting of GridView Control and Changing HeaderText Dynamically (C#)

The following code is my Grid View_AA from aspx page.

<asp:TemplateField HeaderText="Period Name" SortExpression="PERIOD_NAME">
<EditItemTemplate>
<asp:TextBox ID="txtGVPeriodName" runat="server" 
Text='<%# Bind("PERIOD_NAME") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblGVPeriodName" runat="server" 
Text='<%# Bind("PERIOD_NAME") %>' Visible="False">
</asp:Label>
<asp:LinkButton ID="lborganize" runat="server" 
OnClick="lborganize_Click" Text='<%# Bind("PERIOD_NAME") %>'>
</asp:LinkButton>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" Width="400px" />
<ItemStyle HorizontalAlign="Center" Width="400px" />
</asp:TemplateField>
</asp:GridView>

protected void GridView_AA_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
LinkButton lb_pname = (LinkButton)(e.Row.Cells[2].Controls[0]);
lb_pname.Text = "Period Nameāļ°";
e.Row.Cells[2].Controls.Add(lb_pname);
}
}
 
The above code is working well in my project. I hope you can get some references from it. Have a nice day!

Changing default startup page Asp.net

You should put the following code in web.config file:

<configuration>
<system.webServer>
<defaultDocument>
<files>
<add value="index.ashx" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
Please write code to redirect your required page in the page load of  (index.aspx) file. Note that you have to choose correct path of your  start up page if your start up page is located in sub folder of your  project.

protected void Page_Load(object sender, EventArgs e)
{
Response.Redirect("FormBusiness/KPIPerson.aspx");
}
After doing in the above, you can get your project’s start up page as KPIPerson.aspx.

Reference: http://stackoverflow.com/questions/1022949/change-default-startup-page-asp-net-windows-hosted-godaddy