GridView.Databind() not working with object list - c#

The problem I have is this: I have one GridView with a list objects as its data source, it binds with no problem. I have a DropDownList which have the event SelectedIndexChanged, and with this event, I add a new object to the list, after added I want to do a GridView1.Databind() so the new object appears in the GridView.
But this does not work, after I do GridView1.Databind() the data from the GridView gets lost. It even shows the EmptyDataText, but if I refresh the page, everything appears. Also, the DropDownList uses an object list as DataSource and was supposed to use DropDownList1.Databind() but it doesn't work too. It has the same data after the DataBinding, what I did to correct at least the DropDownList was to set the DropDownList again manually.DataTextField = object.attribute1 and DropDownList.DataValueField = object.attribute2. But with the GridView I'm stuck, can someone explain how the method DataBind works with a list of objects?

To refresh a asp:GridView bound to a list set gridView.DataSource property first.
gridView.DataSource = mylist;
gridView.DataBind();
For non-database data it means that list itself must be preserved either in session variable or ViewState.

I remember having this issue a long time ago. I'm guessing the issue is that you don't have the GridView in an UpdatePanel.
If this is the case, the GridView won't refresh and show the changes right away. There's some trickiness about the dropdown placement (I think it has to be in the panel, as well), but I'm sure you can figure it out with some quick searches on UpdatePanel.

Related

How do I connect a binding to a (Winform) ComboBox control to get/set the selection in the control from the binding source?

I have plumbed the deeps of my google-fu to find an answer.
First, I am not simply asking how to bind a combobox to a datasource. It's a bit more than that unless I'm having a serious understanding gap.
On my Winform, I have a DataGridview on the left and on the right I have a panel with values from the selected row on the left. One of those controls on the right is the ComboBox I'm having trouble with. I have my bindingsource and dataview set up and the other controls on the right are working splendidly, except the ComboBox control.
The user, interacting with this ComboBox, should see values such as "Item ABC" and "Item EFG" and the value related to them might be 1234 and 5678. If this was a fully unbound control I'd put an object array of items in. Once I get it working, I'd load that from a different source.
But when I try to DataBindings.Add("??", dataview, "dataviewfield", ...) I can't get the proper value for "??". Runtime debug shows that "SelectedItem.Value" would be the right option, but I get "not found" type exception when I use that. I've tried "SelectedValue" as well, but that didn't work (debug show's it's null & throws no nulls allowed exception).
How can I get that value placed directly into the DataView via the Binding?
Setting the .DataSource simply loses the items and doesn't help at all.
How does one do this? Short of making the ComboBox unbound totally, setting the selectedindex directly and capturing the value when the selected index changes changes - Just seems so clunky to have to do that.
-old programmer
Further Notes: I edited to clarify the placement of the ComboBox.
I have made progress (naturally, only after asking a question does a new avenue pop into my head). I got to thinking I might need a custom binding adapter so started googling that. I found some samples doing what I want.
The foremost problem was I was not using assigning a datasource on the ComboBox, I was simply adding items. When I created a two column dataset and a few rows (could have been anything I suppose) and set that and the two field names as the ComboBox's displaymember and valuemember did the SelectedValue start showing a value (instead of null all the time).
I think that was the problem. The remaining issue is getting the left hand side to re-display/refresh after the change.
When loading the datasource for the DataGridView and the bindingsource, try making them share the same list:
BindingSource1.Datasource = dataset
DataGridView.Datasource = BindingSource1
This should mean that any changes to the data in the bindingsource will also update the DataGridView.
To edit the selected object then just handle the SelectionChanged event and set the BindingSource1.Position to the index of the selected object in the Datasource.

GridView not showing new inserted row

I'm using a gridview and sqldatasource.
When I'm adding a new row in the gridview I need to refresh the page in order to see the new row.
The View state is enabled.
Is there anything I can do ?
Thanks
The DataBind() method of the GridView will reload the data. It is especially useful when you are changing your selectStatement of the SqlDataSource as well. It is a good habit to use this after updating anything that is contained in your GridView.
Call DataBind() on the relevant object (in your case it is probably the SqlDataSource, otherwise is generally the GridView). Doing so should bring up your new record assuming the record actually did get added (you should verify this).
Reference: Control.DataBind Method
Call DataBind() after inserting your new row.

C#.Net : Edit and Update the dynamically bound grid view

In my website I am dynamically binding the datasource to the grid based on the value in dropdownlist.
I want to edit and update the values in grid view and the respective updations should be done in the original database.
How can I do that?
In the most basic and direct way, you use the OnUpdateCommand event of the datagrid to invoke a server site handler. That handler will receive a DataGridCommandEventArgs parameter containing an Item property which is a grid row with updated values. Retrieve key and new values from that row and build a corresponding update command.
do you know how to bind dropdownlist vai datasourase?
and witch data sourse you use tell me first.
else you just make a SELECT query and fill the dataset after that you you must have to bind dropdownlist like this....
ds = dropdownlist.DataBind();
i think this help you.......
else you can tell me if any problem occure in this code..........

how to clear gridview

I want to clear my gridview. I have 2 GridViews and Has Select Button On It. on Selecting this button that item goes into the second gridview. now the question is how should i clear the second grid view. I am trying the clear method but clear method is not found in my visual studio..
dataGridView1.DataSource = null;
or
dataGridView1.Rows.Clear();
gridview.DataSource = null;
//rebind to gridview
gridview.DataBind();
Bind the Gridview to an empty list.
Binding it to 'null' like Patrick Kafka mentioned should work - unless you have some column requirements (I'm mentioning it because I have a tendancy to plug in javascript into my gridviews and unless you specify those columns in the markup, they won't be generated and it will cause errors in the js. (This is also relevant to those getting errors after doing Columns.Clear )
In a case like that (as well as in all other cases), you can simply bind the gridview to a new instance (or empty instance) of your datasource. (Below example for a gridview bound to a datatable - it could be bound to new List<T>() as well).
grdiview1.DataSource = new DataTable();
grdiview1.DataBind();
dataGridView1.Columns.Clear(); //this clears the entire Gridview
Simply add the following c# code to clear the GridView:-
gridView.Rows.Clear();

Prevent AspxGridView from populating on loading page

I have AspxGridView on my page. I want users to be able to set up some data on the web form, and after they press one button, the data from screen is being read, validated and bussines object is being created. This object has a GetData() function, and returns an array of objects representing rows in a grid.
I want ASPXGrid not to populate, until user clicks the button. I know I can set DataSourceId in design time to null – but then I lost availability of synchronizing grid columns with object properties (I cannot add or edit some column properties for new columns). I know I can intercept ObjectCreating event and provide grid with an fake object returning empty data sets. But are there any more elegant solutions?
When are you doing the DataBind() call? Couldn't you just place that inside an if block to make sure it only happens on postback?
if(Page.IsPostBack) {
DoDataBindingStuff();
}

Categories

Resources