List<Tuple<T1,T2>> as DataSource for GridView - c#

I'd like to add a List<Tuple<T1,T2>> as DataSource for my GridView.
"Then do it!"
Yeah, that's not really the problem, the problem is accessing the values inside the GridView.
Here's my Code:
List<Tuple<Group, string>> userGroups = Util.PrepareGroups((string[][])Session["userGroups"]);
gridGroups.DataSource = userGroups;
gridGroups.DataBind();
Throws an exception at DataBind, telling me that Item1.Name doesn't exist, speaking of this, here's my markup:
<asp:GridView runat="server" ID="gridGroups" CssClass="grid gridGroups" AutoGenerateColumns="false">
<Columns>
<asp:BoundField meta:resourcekey="gridGroupsName" DataField="Item1.Name" />
<asp:BoundField meta:resourcekey="gridGroupsFunction" DataField="Item2" />
</Columns>
</asp:GridView>
Needless to say, Item1 is the Group and Nameis a string-Property.
He (yes, he, my IDE is called Bob) obviously doesn't find Item1.Name, is there any way to escape the .?
Thanks,
Dennis

Subscribe to the RowDataBound event in your code-behind, that will fire for each row that is bound and you can use the arguments supplied to the event to get at the current data item - you'll find it easier to get at your values that way than trying to mess around with the databinding syntax etc.
(ps - remember to check the rowtype property in your event handler, that's an easy mistake to make - you'll get unexpected results/errors if you're trying to access the data object for a header/footer row! - the link above has some code that demonstrates this)

Related

Reinitializing the state of gridview after changes done

How can I reinitialize the state of the GridView structure?
For instance, I have defined a set of BoundFields in my aspx page, however, the columns' names and its visibility changes accordingly to conditions.
How can I reset the whole GridView structure, including its column names and visibility to the one I have defined in my aspx page?
Is there a way to do so? Is there a general function to do such or must I define a new function to reset each and every column back to its original state considering I do not only have one Gridview with at least 30 columns in each.
EDIT:
FOR EXAMPLE:
IN MY .ASPX PAGE:
<ASP:GRIDVIEW RUNAT='SERVER' ID='GVEXAMPLE'>
<ASP:BOUNDFIELD HEADERTEXT="ABC" />
<ASP:BOUNDFIELD HEADERTEXT="DEF" />
<ASP:BOUNDFIELD HEADERTEXT="GHI" />
</ASP:GRIDVIEW>
IN MY .ASPX.CS PAGE:
GVEXAMPLE.Columns[1].HeaderText = "this is my new header text";
IS THERE A PREDEFINED FUNCTION IN C# THAT I CAN USE TO RESET THE HEADERTEXT BACK TO ITS ORIGINAL NAMING, 'DEF'?
OR DO I HAVE TO DEFINE MY OWN FUNCTIONS TO RESET ITS NAMING LIKE
GVEXAMPLE.Columns[1].HeaderText = "DEF";

DetailsView binds to columns in database and asp:bound columns resulting in duplicate columns shown?

I have a DetailsView that has a Fields element and some bound columns. The DetailsView is bound to a SqlDataSource. Code on dev and in Environment B is exactly the same, but on dev, I am seeing the columns from the database as well as columns that are bound, so it results in a duplicate columns shown. In Environment B, this is not happening, so I am wondering if it is a data issue, but I am not sure where to start or look?
<asp:DetailsView id="dv" runat="server" DataSourceID="sql" DataKeyNames="Id" OnDataBound="dv_DataBound" OnItemInserted="dv_ItemInserted" OnModeChanged="dv_ModeChanged" HeaderText="Add New" AutoGenerateInsertButton="true">
<Fields>
<asp:BoundField HeaderText="Name" DataField="sName" />
</Fields>
</asp:DetailsView>
The above DetailsView will generate a form that displays label sName with a textbox next to it and another label Name with a TextBox next to it, but the exact same code in Environment B only generates the label Name with a TextBox next to it. This has me thinking it is a data issue, but I can't pinpoint where to start looking. The SqlDataSource.SelectCommand is just a stored proc that is similar to:
SELECT * FROM TABLE_1
Does the DetailsView has something like AutoGenerateColums in gridview?

Get data for DataGridView from something else than properties

I have a DataGridView with a DataSource. The DataGridView auto-generates columns for each property. I want to change this, so that it gets the data for the columns in a different way.
How can I determine how the DataGridView gets the columns and values from the items in the datasource?
It probably uses reflection, do you want to intercept and modify that behavior really?
usually when the default automatic column generation is not enough I disable it and create the columns from code either statically or parsing a config file which could be extended afterwards so to have new columns or differently rendered ones without rebuilding the whole project.
I guess this all depends on what you are looking to do. If you just want to bind to specific columns, and not auto-generate the columns based off of the properties in the DataSource object, then you would do the following:
<asp:GridView ID="GridViewID" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
</Columns>
</asp:GridView>
With "AutoGenerateColumns" set to false, and specific columns set, the GridView will only bind to the properties, in the "DataSource" object, that are tagged in the "DataField" property of the specified columns.
Now, if you are looking to alter these data values, or specifically modify the data as it is being bound, then you would want to attach to the "RowDataBound" event. That way, when the data is bound you can alter it as it attaches to each row.
With the DataGridView.VirtualMode property, you can supply your own datastore:
Virtual mode is designed for use with very large stores of data. When
the VirtualMode property is true, you create a DataGridView with a set
number of rows and columns and then handle the CellValueNeeded event
to populate the cells. Virtual mode requires implementation of an
underlying data cache to handle the population, editing, and deletion
of DataGridView cells based on actions of the user. For more
information about implementing virtual mode, see How to: Implement
Virtual Mode in the Windows Forms DataGridView Control.

How to Bind List<myclass> data with gridview?

Look please my web service codes return type List
i get data from web service with listformat List; also created a gridview below and return list to gridview datasource.Bur eror occurs:
A field or property with the name 'name' was not found on the selected data source.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="Ad" DataField="name" />
<asp:BoundField HeaderText="SoyAd" DataField="surname" />
<asp:BoundField HeaderText="Numara" DataField="number" />
</Columns>
</asp:GridView>
wstest ws = new wstest();
GridView1.DataSource = ws.GetList(); ;
GridView1.DataBind();
Check the contents of your list that you generate. If you do not have a field called name that gets output when you call GetList() then it's going to break because the GridView is looking for it and it's not there to bind to.
You may have to paste some more code for us to verify that, however.
Edit: based on your link of the class you created, it seems that you need to verify that your property "Name" is used with the proper capitalization in all areas. C# is case sensitive so that is most likely why it is being thrown off. Make sure for your other properties as well.
Edit 2: It should also be noted that the confusion may be from the fact that your private members are all lowercase, while your properties are capitalized. Your GridView is going to be binding to the public properties, so that is why you want to make sure the Gridview is looking at the capitalized properties "Name", "SurName", etc.
This line
<asp:BoundField HeaderText="Ad" DataField="name" />
is saying to get a proprty value from each item in teh list, and bind it to this column within the grid. It would seem that the item within the list don't have a property "Name"

Obout Grid filtering a column with text populated by a template

I am attempting to filter a column in an Obout Grid that has been bound to a template.
Background
The DataField of the column is simply a foreign key ID to a History table that contains what are essentially states of a certain object (such as Name, Asset Tag, Serial Number, Additional Info., etc.) If a user were to change a certain state of the object (e.g. Additional Info), a new record would be added to the History table, and this new record is what is referenced by the aforementioned foreign key.
During databinding on the column, I am comparing the most recent history state with the one immediately prior, and returning an English description of what has changed (e.g. "The additional info field of this device has been modified") so that it can be displayed as text in each row.
The Problem
The grid itself seems to only filter the data on the client-side that was part of the original databind (in this case, simply the foreign key ID to the history record). I am able to filter by this number, though it does not actually show up in the column view because it has been replaced by the English description.
Grid Markup
<cc1:Grid ID="grdHistory" runat="server" AutoGenerateColumns="False" DataSourceID="dsHistory" AllowFiltering="true">
<Columns>
<!-- other columns snipped -->
<cc1:Column DataField="DeviceHistoryID" HeaderText="Event description" Width="450" Wrap="true"
Index="3">
<TemplateSettings TemplateId="tplEventDescription" />
</cc1:Column>
</Columns>
<Templates>
<cc1:GridTemplate ID="tplEventDescription" runat="server">
<Template>
<%# FormatEventDescription(Container.DataItem) %>
</Template>
</cc1:GridTemplate>
</Templates>
</cc1:Grid>
FormatEventDescription(Hashtable Records) is the function that returns the English version of the changes to the object's state that I wish to filter on.
Question
Is there a way to filter by the English description in the column? At this point, any insight (design flaws, etc.) is welcome. I have tried both sifting through all of their documentation and even attempted to contact their support regarding this issue but have yet to receive a response.
I don't know much about the Obout's 3rd Party control, but appears to be subclassed from the .net GridView control. So you might ask the question again, but instead use gridview where the word Obout is, because apparently not many people use that particular control.
After briefly scanning the events of the grid, I think you might try using the RowDataBound event to do the necessary transforms.
Also you might be able to use the Selected event of the datasource to transform the data.
If you are able to transform the data to it's final readable state earlier, you may be able to filter it earlier in the chain of events that occur.

Categories

Resources