gridview databinder.eval question - asp.net

We've got a number of gridviews that we're populating with data from an ADABAS datasource via some middleware that, unfortunately, gives us back lots of arrays of stringBuilder that we turn into a datatable, set as the datasource for a gridview, then bind.
It's convoluted, but I'm in a situation where I need to be able to update the gridview (say, drop a row), without going back to the mainframe, deleting the row, and regenerating the datatable, as I would if we had an Oracle datasource.
So, I'm trying to hack something together where on serverside I extract the data from the gridview, build a datatable, delete the given row, then use that datatable as the datasource for the gridview, and rebind.
I can do it on a case by case basis, so long as I know the layout of the gridview. But we're going to be doing this over and over on this current project, I'm trying to figure out a way to abstract it so that I can use it for any gridview I throw at it.
The problem I'm getting is that most of the cols in the gridview are templatefields with asp:labels, where the data is put in with the databinder.eval thing:
<asp:TemplateField HeaderText="Code">
<ItemTemplate>
<asp:Label ID="lblCode" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "code")%>'></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblTitle" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "title")%>'></asp:Label></ItemTemplate>
</asp:TemplateField>
So, when building my 'fake' datasource, I need to be able to extract from somewhere in the gridview those column names.
Is that information available in the gridview?

Yikes. You are going to need to store the fake datasource column names within the GridView along with the column values. One suggestion would be to add a HiddenField along with each fake datasource field value. In the example below, display the code (value) in the Label and the column (name) in which code maps in the HiddenField:
<asp:TemplateField HeaderText="Code"
<ItemTemplate>
<asp:Label ID="lblCode" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "code")%>'></asp:Label>
<asp:HiddenField ID="hfCode" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "codefield")%>'></asp:HiddenField>
</ItemTemplate>
</asp:TemplateField>
It's ugly, but the column names will always be available to you -- no matter what they might be. Best of luck.

Related

asp.net data controls

I have a table with the following structure,
I need to restructure the table to show the CustomSpaceName in the following order,
Space3 Personal Case Quick case
Space1 Space2
For each entry I will create a link button and pass the CustomSpaceId in query string.
So which is the ASP.NET Data Control matches best with my requirement. I think using the loop and generate table structure is a BAD idea.
No Need of doing that with a old method when ASP.net gives you GridView and other Data Bounding controls
Basically gridview will do the same operation that you told in a efficient way.
You can use the in-built methods.
Grid View
Repeater
ListView
DataList
Here I will recommend DataList.
Use RepeatColumns="4" property.
<asp:DataList ID="DataList1" RepeatColumns="4" runat="server">
<HeaderTemplate>
<asp:Label runat="server" ID="lbl1" Text='Header'></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" ID="lbl1" Text='<% Eval("CustomSpaceName ") %>'></asp:Label>
</ItemTemplate>
</asp:DataList>

How to display a large amount of data in gridview

I am trying to show a large amount of data in gridview but the problem is that everytime data increase the gridview row size increase automatically.
is there any possible way that the data which is stored in my MS access Database display in multi line instead of one single long line.
If you are populating the GridView using AutoGenerate="true" make if AutoGenerate="false"
Then use asp:TemplateField to populate the GridView.
Now give an ItemStyle-Width and ItemStyle-Wrap.
<asp:TemplateField ItemStyle-Width="50px" ItemStyle-Wrap="true">
<ItemTemplate>
<asp:Label ID="ShipNameLabel" runat="server" Text='<%# Eval("ShipName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
The telerik gridview control supports multi line rows http://www.telerik.com/products/aspnet-ajax/grid.aspx.
However, this does cost. You may try displaying only a few columns, then the user has to click on the row to see the other information in a form view.
Don't understand your question fully but it sounds as a Repeater might work better for you where you have more control of the layout of the rendering.
If it is the amount of rows that are the problem I do recommend to introduce paging to limit amount of row displayed at once.
Update:
1) Set up a CssClass for the GridView itself and include the table-layout:fixed style. This tells the browser that you're going to specify the width of each cell. You may also want to include the overall width of the grid here as I mention in (3).
2) The first row of the table sets the width for each cell, and that's usually the HEADER row, not the item row, so use either HeaderStyle-CssClass or HeaderStyle-Width to set the width of the cell.
3) Make certain the table itself is wide enough to hold all of the cells. I added up all of my cell widths and used that to set the width via the CssClass attribute on the GridView.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns='false'>
<Columns>
<asp:BoundField DataField='Name' />
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat='server' ReadOnly="true" BorderStyle="None"
TextMode="MultiLine" Text='<%# Bind("Description") %>'
>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You may have to add some styling to gridview via .skin or css

Showing number in 2 decimal places in GridView

I have one GridView in a .aspx page, and am showing dynamic data in this grid, setting AutoGenerateColumns="True".
Depending upon which of the options the user selects in a combo box, I am binding different DataTables to the GridView. For example, if the user selects Persons then I am fetching the Persons DataTable, and if the user selects Products then I am fetching Products DataTable.
How can I show a float or double number in 2 decimal places in GridView?
The bound column should have a DataFormatString column. You could do something like:
DataFormatString="{0:0.00}"
Numeric Custom Format Strings
UPDATE
In the case of AutoGenerateColumns="true"... I'd have to know more specifics about what you're binding, but here are some avenues to explore:
I'm not sure if GridView will
respect the DataFormatAttribute in
Data Annotations. If you are binding
an object, and GridView respects
that attribute, that might be one
route to go.
Wire the RowDataBound event and
inspect each column for potential
decimal values, and format that way.
you can write BoundField in GridView:
<asp:BoundField DataField="amount" DataFormatString="{0:n}" />
you can also write TemplateField in GridView
<asp:TemplateField>
<ItemTemplate>
<%#Eval("amount","{0:n}")%>
</ItemTemplate>
</asp:TemplateField>
You can do DataFormatString="{0:n2}"in your boundfield
This works on a template column, say if you want a decimal out to two places for a ratio (like 1:3)
<%# Eval("somedatacolumn", "1:{0:.##}").ToString() %>
If you use DataFormatString and it does not seem to be doing the trick, add HtmlEncode = "false", for example:
<asp:BoundField DataField="DateScheduled" HeaderText="Date Created" DataFormatString="{0:D}" HtmlEncode="false"/> // date format
<asp:BoundField DataField="Amount" HeaderText="Pay This Amount" DataFormatString="{0:F}" HtmlEncode="false"/> // number format
There are two easy ways to format things in a GridView. The first is given in a previous answer - use the DataFormatString. The second, which sounds like it applies to your situation, where you are dynamically loading the grid, is to change the data going into the grid.
So, rather than returning a number and trying to format it, return a formatted number and let the GridView display it.
<asp:TemplateField HeaderText="Prev Salary" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblGrdSalary" runat="server" Text='<%#Bind("Salary", "{0:n}") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" Width="70px" />
</asp:TemplateField>

Multiple databound controls in a single GridView column

I have a grid view that is data bound to a dataset. In a grid I have a column DefaultValue, which has three controls in it - a dropdownlist, a checkbox and a textbox. Depending on the data that is coming in it can switch to any of these controls. Everything is simple enough when we need just to display data - in gridview_prerender event I simply make one of the controls visible. The control is setup like following:
<asp:TemplateField HeaderText="Default Value" SortExpression="DefaultValue">
<ItemTemplate>
<asp:TextBox ID="txt_defaultValue_view" runat="server" Text='<%# Bind("DefaultValue") %>' Enabled ="false" />
<asp:DropDownList ID="ddl_defaultValue_view" runat="server" Enabled ="false" />
<asp:CheckBox ID="chk_defaultValue_view" runat="server" Enabled ="false" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_defaultValue_edit" runat="server" Text='<%# Bind("DefaultValue") %>'/>
<asp:DropDownList ID="ddl_defaultValue_edit" runat="server" />
<asp:CheckBox ID="chk_defaultValue_edit" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
My problem starts when I am in edit mode and I need to update the grid with new data. Since only the textbox control is databound, the RowUpdating event can only access data from the textbox column and all of my other data simply gets discarded. I also can't databind with checkbox and dropdownlist control, since they can get an invalid data type exception. So, does anyone know how can I update a column that has three different controls, where each of these three controls might have a valid data?
Thanks
First, i would switch visibility of the controls in RowDataBound of the Grid and not on PreRender, because it can only change on DataBinding and not on every postback.
You can there also bind the Dropdown to its datasource, change the checked-state of the Checkbox and set the Text-property of the Textbox according to the the Dataitem of the Row.
When you update you can find your controls with FindControl in RowUpdating and get their values(f.e. Dropdownlist.SelectedValue).
GridView has pair of events:
RowUpdating/RowUpdated
RowDeleting/RowDeleted
....
In your case your need RowUpdating - is called right before update is performed, So find in row expected control (saying checkbox) and make preparation before pass to database
As another way review possibility to use ObjectDataSource event Updating - it is usual way to specify parameters for query execution.

custom *arbitrary* TemplateField definitions

I'm building a GridView on the fly, and I'd like to pre-define the TemplateFields to be included ondemand. So, what I'd like to do is have a declarative file that defines how the different templates look for a specific column. Like:
<asp:TemplateField>
<HeaderTemplate>
this is a text column
</HeaderTemplate>
<ItemTemplate>
data goes here
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Text="databindhere" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
this is a bool column
</HeaderTemplate>
<ItemTemplate>
if(true) "yes" else "no"
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox Checked="databindme" />
</EditItemTemplate>
</asp:TemplateField>
So, if my query had a text and two bool fields, I could push the appropriate TemplateFields in the the Columns property as needed. (I hope I'm making sense here)
So, how would I go about creating declarative files for the above definitions? And then, how would I reference those definitions programmatically?
Ok, what would work best is to subclass System.Web.UI.WebControls.TemplateField, but when I do that, I can't seem to use the object with the <%# Register %> directive. If I could, I'd create a handful of UserControls with the new derivative, and then LoadControl() and Add() them to the Columns of my grid as needed.
Any ideas?

Resources