How to Bind List<myclass> data with gridview? - c#

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"

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";

asp GridView add data to a certain column (after TemplateField) with Boundfield?

I have a asp:GridView wich in code behind is asigned to a List with Objects.
gwMyGridview.DataSource = test; // test is a list with some objects (two fields)
gwTract.DataBind();
I can add buttons and stuff to the columns, but my data (two columns) are always the last two columns. I know BoundField can be used, but I still get two columns in the end.
How can i do this: column1 = data1, column2 = button, column3 = data.
Now it looks like this: button, data, data
Thanks in advance!
Don't let the GridView to auto generate the columns. To do this, follow these steps:
1- Click on the GridView's Tasks (little arrow on top of the GridView when you click on it) and click on Edit Columns, and uncheck the "Auto-Generate fields" field, or from the Markup set the AutoGenerateColumns property to false
2 - Add your columns manually to the GridView, by either adding them in the previous dialog (Fields) or in the markup, in the exact order you want them to a appear in your GridView:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="data1" HeaderText="Data1" />
<asp:TemplateField HeaderText="CustomColumn"></asp:TemplateField>
<asp:BoundField DataField="data2" HeaderText="Data2" />
</Columns>
</asp:GridView>
After that you can bind normally.

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.

List<Tuple<T1,T2>> as DataSource for GridView

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)

How to bind entity model to WinForms DataGridView with specific column names?

Currently I am doing:
var items = from t in entity.Items
select new
{
Name = t.ItemName,
Description = t.ItemDescription
};
myDataGridView.DataSource = items.ToList();
The problem is that when bound to the DataGridView, I get two columns of "Name" and "Description". I want to rename these to "Item Name" and "Item Description" (example).
If I add an unbound Column to the datagridview, it just gets displayed along with my columns. I can not seem to create a databound column.
Can you post the grid's source?
You'll need AutogenerateColumns="false" to add columns manually.
To change the header text, you should be able to do the following:
<Columns>
<asp:BoundField DataField="Name" HeaderText="Item Name" ReadOnly="True" />
// .. etc
I found out how to use drag and drop of an object data source to bind the columns to a DataGridView directly, which allows me to change the header text.

Categories

Resources