GridView Row Number using Markup (only) - c#

I have a requirement to display row number in grid view. What is the best way to display the row number using BoundField or TemplateField?
Note: This need to be done using markup only (without code behind).
Note: When sorting happens, the row number should not be in sequence, the first row should go down with its content.
I have already referred the following:
http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/2eead3e3-5cc2-40f7-a91c-8f7942d5329c/

<asp:TemplateField HeaderText="#" >
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
By the way, this solution proposed in article you referred. Why you don't like it and ask here?

The best place to do this would be to use the templatefield
<asp:TemplateField HeaderText="Row Number">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
An examples http://www.devcurry.com/2010/01/add-row-number-to-gridview.html
The BoundField displays the value of specified DataSource field as text.
The TemplateField allows to mix html or make use of web controls.
Please refer to the following explanation to confirm the difference.
http://forums.asp.net/t/1804988.aspx/1

The gridview is rendered as html table. If you don't want to calculate the row number in code behind, you should use JQuery.
var rowCount = $('#myTable tr').length;
And you should fill the table footer with rowCount value.

Related

Merge column header in gridview

I've been try to merge the header column from 2 into 1, but the example and i already tried it with something like this
GridView.Controls[0].Controls.AddAt(0, oGridViewRow); the question and the example that floating around was just adding 1 column, and add it in the top of the old column, not merging it. So i want to actually merging 1 column and the rest of the column is still the same. Here is the picture of gridview column that i want to merge :
I want to Merge the "Action" column header from 2 into 1. So below the action column, there will be an edit and delete.
Oh i almost forgot, Here is the gridview code that handle the action column :
<asp:CommandField ShowEditButton="true" HeaderText="Action"/>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="linkDelete" runat="server"
CommandArgument = '<%# Eval("XXX")%>'
OnClientClick = "return confirm('Do you want to delete?')"
Text = "Delete" OnClick = "DeleteDetail"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Although I am strictly against hard-coding, this is what I have, you can write this in 'RowDataBound' event of gridview:-
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[3].ColumnSpan = 2;
e.Row.Cells[4].Visible = false;
e.Row.Cells[3].Text = "Action";
}
Here, You need the change the cell index according to your gridview design.
You can use Templates to customize how your column header and data look like
The below is a very simplified example that should show the desired result
But you will need to work on hadnling Edit and Delete actions your own way
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<div> Action </div>
</HeaderTemplate>
<ItemTemplate>
Edit | Delete
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
As you see, my idea is not merging the headers of both columns. Rather, I put the data of of both columns in a single column under single header. I think it achieves the same result.

ASP.NET non-html escaped string to GridView

I have GridView with my DAO as data source rendering some person data (name, surname...). I want to add to rendered table simple link to expanded view with more informations. Ithought I could add html link with POST argument to each row. But every html I try to pass to GridView gets escaped. Can I somehow unescape it? Or is there any other simple way?
It is my private, very quick project, I donĀ“t need any robust solution. Just the simplest and quickest one. Thanks.
It sounds like you want to use an ItemTemplate in your gridview to render HTML controls within the grid:
asp:GridView ID="myGrid" runat="server">
<Columns>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<!--Any HTML can go here, below is a label -->
<label><%# DataBinder.Eval (Container.DataItem, "lastname") %><label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

File Size column with kb in sharepoint

I am using spgridview to display the items in a library based on library viewid. I am assigning the datasource as SPlist.items.getdatatable();. Here for file size column, I am getting the values in number. But i need to show in KB's.
How can i show the filesize values with kb,mb as suffix without looping each column in the datatable?
Thanks in Advance
Can you try this.... ?
<asp:TemplateField HeaderText="HeaderText">
<ItemTemplate>
<asp:Label ID="lbl" runat="server" <%# Convert.ToString(Eval("ColumnName")/1024) + " KB" %> ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
As i know,
We can't do. The alternate is to do looping and apply validation for each row data.

How to create column that will autogenerate the serial number for Each row of a Gridview?

I have a Gridview with a list of Items. I want to show the Serial Number in One Column of a GridView like 1 2 3 4 5.....
You can do using Container.DataItemIndex in GridView. It will display serial numbers as above mentioned.
<asp:TemplateField HeaderText="SNo.">
<itemtemplate>
<%#Container.DataItemIndex + 1 %>
</itemtemplate>
</asp:TemplateField>
I would suggest adding this data to the source you are using to bind to the GridView. So If you have a ICollection<Product> that you are binding to the GridView, add the Serial number to the product class.
Alternatively, you could turn the ICollection<Product> into a Dictionary<int, Product> and bind the key to the Serial number column.
It's difficult to know what would suit without knowing how you are biding your data.
Add this Template field inside the tag Columns. You will get AutoGenerate SNo.
For More Information see the below link
How to create autogenerate serial number column in GridView
<asp:GridView ID="GridEmployee" runat="server" AutoGenerateColumns="false"
CellPadding="5" Style="background-color: rgb(241, 241, 241); width: 50%;">
<Columns>
<asp:TemplateField HeaderText="S.No">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I hope this helps you. Thanks
Here is simple way...
<ItemTemplate>
<%#Container.ItemIndex + 1 %>
</ItemTemplate>
Googling for serial number column gridview will give you millions of results that discuss the same thing. Read the articles/tutorials/discussion and implement it.
Please do Google before you ask.

Working with GridView and ItemTemplates (ASP.net/C#)

I have a GridView that is populated via a database, inside the GridView tags I have:
<Columns>
<asp:TemplateField>
<ItemTemplate><asp:Panel ID="bar" runat="server" /></ItemTemplate>
</TemplateField>
</Columns>
Now, I want to be able to (in the code) apply a width attribute to the "bar" panel for each row that is generated. How would I go about targeting those rows? The width attribute would be unique to each row depending on a value in the database for that row.
<asp:Panel ID="bar" runat="server" Width='<%# Eval("Width") %>' />
If you like, you can change Eval("Width") to the expression that calculates the width.
You are going to want to handle the GridView's RowCreated event. This occurs just after the GridView creates each row and will give you programmatic access to the row and all the controls contained within it.
Here is the MSDN page on the event.
I suggest you to not use Eval, if you can, because it's a little slower. In that cases I generally prefer to cast my data source to his base type:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Panel ID="bar" runat="server" Width='<%# ((yourCustomData)Container.DataItem).Width %>' />
</ItemTemplate>
</TemplateField>
</Columns>
where yourCustomData is the row type of your data source (i.e. the element of a List<>).
This method is really faster than Eval.
Edit: oh, don't forget to include in the page a reference to the namespace that contains yourCustomData
<%# Import Namespace="yourNameSpace.Data" %>
I suggest you attach a callback handler to the OnRowDataBound event. Each row binding will fire an event which you can handle in your callback handler.
In that callback handler, you can get the info about the item being bound to the row and apply the width according to the values in your database.

Categories

Resources