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.
Related
I have a process that pivots data to create columns based on the UniqueIdentifier key value. The resulting data table has column names such as:
> PartNum
> [DB1A6498-7CC6-4EA0-846A-9B6EAB771777]
The portion of ASP is:
<asp:TemplateField HeaderText="Part No.">
<ItemTemplate>
<asp:Label ID="lblPartNum" runat="server" Text='<%#Eval("PartNum") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Run Out">
<ItemTemplate>
<asp:Label ID="lblRunOut" runat="server" Text='<%#Eval("DB1A6498-7CC6-4EA0-846A-9B6EAB771777") %>' />
</ItemTemplate>
</asp:TemplateField>
The Part Number displays just fine, however field based on the uniqueID results in a message:
DB1A6498-7CC6-4EA0-846A-9B6EAB771777/ is neither a DataColumn nor a DataRelation for table .
I read through the data table columns to create the grid columns, which display fine, however when attemtping to dynamically create the ItemTemplate, the error occurs. Putting the brackets "[]" around the uniqueID in the label did not work either.
Any suggestions are greatly appreciated. Thanks
Kevin...your response led me down the path of the brackets. Based on that, I found information about the DataBinder with the final result being that the Text property of the asp:label reads:
Text='<%# DataBinder.GetPropertyValue(Container.DataItem,"[DB1A6498-7CC6-4EA0-846A-9B6EAB771777]") %>'
This is displaying data from the uniqueID fields with no errors.
I have usedgridView for update and delete data. But I'm getting
"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index" error while updating.
There is no problem in your jquery datetimepicker
Problem seems to be in :
GridViewRow row = GridView1.Rows[e.RowIndex];
Simply take separate link buttons for edit and delete as:
<asp:TemplateField HeaderText="Edit" ItemStyle-Width="150">
<ItemTemplate>
<asp:linkbutton id="lnkEdit" runat="server" CommandName="Edit" Text="Edit"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete" ItemStyle-Width="150">
<ItemTemplate>
<asp:linkbutton id="lnkDelete" runat="server" CommandName="Delete" Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
I can see you are using dropdownlist for copy_id so it is better to use the below code to find the dropdownlist and fetch value from it.
DropDownList lblCopyDdl= (DropDownList )GridView1.Rows[e.RowIndex].FindControl("lblCopyId");
int Copy_Id = Convert.ToInt32(lblCopyDdl.selectedvalue);
Recommendation::
Better if you use the same code as mentioned above for other fields for getting value..
for eg
int Borrower_Id = Convert.ToInt32((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtBorrowerId").Text);
Update::
If it not works then do check your e.RowIndex as mentioned in Ángel Di María's answer.
Update 2
try to fetch the date field like this
DateTime Date_Borrowed = Convert.ToDateTime((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDateBorrowed").Text);
and
DateTime Date_Returned= Convert.ToDateTime((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDateReturned").Text);
It seems that you are trying to read a cell value using index but there is no data element at that position. Suggest you to do debug and increment index value in your collection in watch window and observe data
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.
Well I am having a very specific requirement in my current project.
I need to add path values (as string) in a DataGridView. So the cell must show the path in format PathEllipsis i.e. similar to this -
d:\abcd\efgh\ijkl\mnop\qrst...\test.txt\0F\GHIJ\KLMN\OPQR\STIV\WXYZ\test.txt.
I don't know if I am searching it correctly, but I am unable to find relevant information. Any Idea?
One workaround that I know is to manually edit the path, but for that, I'll have to keep a hidden column or Tag. The process will be tedious and I don't want that...
Help!!!
Later Edited - Yes, the beginning and the end of the path must suit to the length of the cell if path length is wider than the cell.
Here i am posting the solution that i used for my project.
<textbox> for dynamic data and tag for static data
<asp:TemplateField HeaderText="Path Details" ShowHeader="False">
<ItemTemplate>
D:\FolderName\YourFileName.txt
<asp:TextBox ID="txtpath" TextMode="MultiLine" runat="server" ReadOnly="true" Text='<%# (string)Eval("Database Field") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
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.