Inserting path in DataGridView cell - c#

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>

Related

Create Dynamic ItemTemplate based on GUID column names

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.

TextBox with TextMode="Date" does not show data

I have a Gridview, with a DateTime column defined like this:
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="txtDate" Text='<%# Item.Date.ToShortDateString() %>' runat="server" TextMode="Date"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblDate" runat="server" Text='<%# Item.Date.ToShortDateString() %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
The date gets displayed in the grid, and it gets saved correctly when I edit a row from the grid. The only problem is that "txtDate" doesn't display the current value when entering in "edit mode" of a row. It displays "mm/dd/yyyy" instead of the actual value coming from the bind. And I don't know why. Nonetheless, when I remove the property TextMode="Date", the actual value displays correctly.
Any ideas? Thank you!
This occurs in Google Chrome when the browser can't parse the given date. You should be getting a warning about that in the browser's console as well. Google Chrome expects the format to be yyyy-MM-dd, so e.g.:
Item.Date.ToString("yyyy-MM-dd");

using C# in markup to get value from a datagrid

I want to start off by saying I am not sure how to properly word what I am aiming to do, so there may be plentiful examples on how to do what I am trying to do, I just cannot find them.
That being said my explanation might be a bit wordy so I appreciate any and all help.
I am including a new feature on a previously created .Net webpage and sql database. The code currently returns a DataSet from the database and binds it to a datagrid. From there it is loaded into a table on the webpage. What my new column needs to do is take the primarykey from the current row and use it to call a stored procedure to get more data.
<asp:TemplateColumn SortExpression="siteAdmin" HeaderText="SITE ADMIN">
<ItemTemplate>
<asp:Label runat="server" Text='<%# listAdmin("siteAdmin", ***issue is here*** ) %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
The code in the markup calls a method that calls the stored procedure I need to retrieve the string I am looking for. The issue I am having is figuring out how to get the primarykey, which is an int into the second parameter of the listAdmin() method. The primarykeys for each row are returned from the database in a column of the dataSet called "trackingId"
Any help would be gratefully received. If I can clear anything up, please ask.
You could try the following:
<asp:TemplateColumn SortExpression="siteAdmin" HeaderText="SITE ADMIN">
<ItemTemplate>
<asp:Label runat="server" Text='<%# listAdmin("siteAdmin", Eval("trackingId")) %>'>
</asp:Label>
</ItemTemplate>

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.

Delete Record from GridView AND File from server in one click

I'm trying to delete a record from Gridview1 at the same time delete the corresponding image file off the server in a single click. (Each row in Gridview1 has an associated image file on the server.)
To delete the record, I'm using asp:CommandField showDeleteButton="true" along with a sqlDataSource's DELETE statement.
During that process, I'm also using GridView1's "onRowDeleting" event to delete the corresponding image file off the server.
Here's what it does, with the code I have below:
The record indeed gets deleted,
The file on the server does not,
There are NO errors thrown (since I guess that it's not finding the file, and this is the expected behavior).
Also consider:
I've already set up and tested a much simpler "see if files can actually be deleted off the
server" test before I started development on the Gridview. Since our files are on a hosting
company server, I wanted to test for any permissions issues.
Whereby:
Enter the fileName and extension into a text box ("myImage.jpg")
Click the button that uses the File.Delete method
WhaaLa - the file is gone from the server.
However, I can't get the file to go away with my new setup.
Here's the code:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="libraryID"
DataSourceID="SqlDataSource1" Width="800px" onrowdeleting="deleteImageFromServer" CssClass="gridViewSmallText"
OnDataBound="rowCount">
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<%--A link that goes to the uploadPage to upload a new version of the image--%>
<asp:HyperLinkField runat="server" HeaderText="SKU (Click to Update)" DataTextField="sku" DataNavigateUrlFields="sku" SortExpression="sku" DataNavigateUrlFormatString="graphicUpload.aspx?sku={0}" >
</asp:HyperLinkField>
<asp:TemplateField HeaderText="Image" SortExpression="imagePath">
<ItemTemplate>
<%--Pull the imagePath column from the database here-it also includes the image file --%>
<asp:Image ID="merchImage" runat="server" Height="100px" ImageUrl='<%# "http://www.ourcompanysite.net/" + DataBinder.Eval(Container.DataItem, "imagePath") %>' /><br />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="View Full Size">
<ItemTemplate>
<%--A link to view the image in it's full size in a new browser window--%>
<asp:HyperLink ID="fullSizeHyperlink" runat="server" NavigateUrl='<%# "http://www.leadingjewelersguild.net/" + DataBinder.Eval(Container.DataItem, "imagePath") %>' Text="View" Target="_blank" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DateUpdated" </asp:BoundField>
<%---some date stuff here--%>
<asp:BoundField DataField="DateCreated" HeaderText="First Uploaded" SortExpression="DateCreated" >
</asp:BoundField>
</Columns>
</asp:GridView>
Code behind:
protected void deleteImageFromServer(object sender, GridViewDeleteEventArgs e)
{
// I read from GridViewGuy.com that you're supposed to reference the row item via e.Values
string imageToDelete = e.Values["sku"] + ".jpg";
//I pulled the value of "imageToDelete" into a lable just to see what I was getting
//during the "onRowDeleting" and it reported back .jpg instead of the full file name
//myImage.jpg, so I guess this is the crux of my problem.
string image = Server.MapPath("/images/graphicsLib/" + imageToDelete);
string image = Server.MapPath("e:\\sites\\oursite\\files\\images\\graphicsLib\\" + imageToDelete);
if (File.Exists(image))
{
File.Delete(image);
}
//at this point the record from GridView1 is gone, but the file on server remains.
}
I think part of your problem may be that in order for e.Values["sku"] to contain a value, it has to be bound first. I don't think HyperlinkField binds its data (I could be wrong on that though so don't quote me)
First try adding a <asp:BoundField DataField="sku" Visible="false" /> in your column list. or change the HyperLinkField to TemplateField and explicitly bind the sku '<%#Bind("sku")%>'
If that doesn't work you can try changing DataKeyNames="libraryID" to DataKeyNames="libraryID,sku". You should be able to get the value out of e.Keys["sku"], or e.Values["sku"].
After you started development using the GridView control, have you verified that the deleteImageFromServer method is being called by setting a breakpoint in there?
And that imageToDelete is being set correctly?
After breaking into it, look at the 'watch' values for 'sender' and 'e'. Drill down into those to see what kind of objects they are and what values they hold.
Sometimes you have to traverse the object hierarchy in the GridVied control to get to the right object.

Categories

Resources