Home > ASP.net C#, General, tricks > GridView in asp.net 3.5 c#

GridView in asp.net 3.5 c#

Hi All,
Today I learned Grid View:
Here I am assigning LINQ as datasource.

.aspx file
You need to include it in form tag.

<asp:GridView ID=”GridView1″ runat=”server” AllowPaging=”True” CellPadding=”4″
ForeColor=”#DEDEDE” BackColor=”#E7E3E3″ GridLines=”None”
AutoGenerateColumns=”False”  AllowSorting=”True”
onpageindexchanging=”GridView1_PageIndexChanging” BorderWidth=”1px”>
<RowStyle BackColor=”#F7F6F3″ ForeColor=”#333333″ />
<Columns>
<asp:TemplateField HeaderText=”Sr. No”>
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText=”Group Name” DataField=”GroupName” />
<asp:BoundField HeaderText=”Status” DataField=”Status” />
<asp:HyperLinkField HeaderText=”Action” Text=”Edit” />
</Columns>
<FooterStyle BackColor=”#5D7B9D” Font-Bold=”True” ForeColor=”White” />
<PagerStyle BackColor=”#284775″ ForeColor=”White” HorizontalAlign=”Center” />
<SelectedRowStyle BackColor=”#E2DED6″ Font-Bold=”True” ForeColor=”#333333″ />
<HeaderStyle BackColor=”#5D7B9D” Font-Bold=”True” ForeColor=”White”
BorderStyle=”Dotted” BorderWidth=”1px” />
<EditRowStyle BackColor=”#999999″ />
<AlternatingRowStyle BackColor=”White” ForeColor=”#284775″ />
</asp:GridView>

.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
public void bindGridView()
{
List admGrp = GroupService.Instance.GetAllGroups();
GridView1.DataSource = admGrp;
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
int newPageIndex = e.NewPageIndex;
GridView1.PageIndex = newPageIndex;
bindGridView();
}

Note: You need to set AutoGenerateColumns = “False” explicitly as by default its True.
When the AutoGenerateColumns property is set to true, an AutoGeneratedField object is automatically created for each field in the data source. Each field is then displayed as a column in the GridView control in the order that the fields appear in the data source.

1) Also, If you want to add serial number or Row Index, use following code:

<asp:TemplateField HeaderText=”Sr. No”>
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>

2)If you need to provide dynamic hyperlink then,

<asp:HyperLinkField HeaderText=”Action” Text=”Edit” datanavigateurlfields=”GroupId”
datanavigateurlformatstring=”addGroups.aspx?action=edit&grpid={0}” />

Note,  we need to  include datanavigateurlfields.

You can  also  add  tags DataTextField & DataTextFormatString to  show dynamic text.

Also, You can add more than one parameters as follows:
<asp:HyperLinkField HeaderText="UserName" DataTextField="LoginId" SortExpression="LoginId" datanavigateurlfields="LoginId,Password" datanavigateurlformatstring="getUserDetail.aspx?uid={0}&pass={1}" HeaderStyle-ForeColor="#164b6d" />


If you want to disable particular link depending upon some condtion(in my case it depend upon status),
you can use following code: e.Row.Cells[2].Enabled = false;(where Cells[2] is location of Hyperlink)

3) If you need  to display dynamic text base  upon  text  fetched  fromDb  or  change  the color   of cell depending  on data,you need to include  following code:

Here  e.g  I am  showing status = Active or Inactive  base  upon the flag  value stored in DB.

In .aspx  file  we need to write as usual,

<asp:BoundField HeaderText=”Status” DataField=”Status”  />

Also additionally we need to include an event,    OnRowDataBound=”GridView1_RowDataBound”

In aspx.cs,

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs  e)
{
// Check if row is data row, not header, footer etc.
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Get value of third column. Index is zero based, to
// get text of third column we use Cells[2].Text
int CellValue = Convert.ToInt32(e.Row.Cells[2].Text);

// If value is greater of 10, change format
if (CellValue == 1)
{
// Use this syntax to change format of complete row
//e.Row.BackColor = System.Drawing.Color.Yellow;
// Use this syntax to change format of single cell
// e.Row.Cells[2].BackColor = System.Drawing.Color.Red;
e.Row.Cells[2].Text = “Active”;
}
else
{
e.Row.Cells[2].Text = “Inactive”;
}
}
}

4)

Disable Edit Button if condition in another column is met

Chk :  http://forums.asp.net/p/1513875/3621570.aspx

5)

If you want to provide fixed  length to grid view column:

Actually  I  faced  the same  problem.I got the solution from site mention below:

http://www.pluralsight-training.net/community/blogs/keith/archive/2008/07/30/controlling-column-width-in-a-gridview.aspx

To fix the issue you need to do following steps:

1) Set up a CssClass for the GridView itself and include the table-layout:fixed style.

2) Use either HeaderStyle-CssClass or HeaderStyle-Width to set the width of the cell.

B’coz the width of the column  is set  by the first row, which in  most of the cases is Header.

3) Include following  style into your CssClass for the GridView TD(e.g. In my case it was .mGrid td)
word-wrap : break-word ;
word-break : normal;

Hope it will solve your problem.

Ref Links:

http://www.asp.net/data-access/tutorials/paging-and-sorting-report-data-cs
http://msdn.microsoft.com/en-us/library/aa479347.aspx

http://www.beansoftware.com/ASP.NET-Tutorials/Conditional-Values-Styles-GridView.aspx

http://www.c-sharpcorner.com/Forums/ShowMessages.aspx?ThreadID=80853

Today I had following issue:
“how to access hidden fields from gridview in asp.net”
I need to set hidden ID value & need to access it.(I need to access uid(primary key) of record), I got following solution.
Previously I was trting asp:BoundField with Visible=”false”,but wasa not able to access the values.The problem is that when the Visibility property of the BoundField is set to false the column isn’t rendered to the client.

The I tried following:
[CODE]

<asp:HiddenField ID="HiddenField1" runat="server" Value='' />

[CODE BEHIND]
In RowDataBound function
if (e.Row.RowType == DataControlRowType.DataRow)
{
HiddenField hidden = (HiddenField)e.Row.Cells[6].FindControl(“HiddenField1″);

}

hidden.Value will give text data.

Reference:

http://stackoverflow.com/questions/1833532/get-hidden-field-value

Categories: ASP.net C#, General, tricks
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.