GridView not showing new inserted row - c#

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.

Related

Updating the DataSource of a DropDownList in ASP.NET's C#

I have an edit form that uses a DropDownList, connected to a SQL server table using a DataSource. This edit form allows users to set the table's "IsDeleted" column value to 1, which would then hide it from all of the application's queries. (So the transaction still exists in the database, but not in the application)
The problem I've encountered with this is that if the page on which the edit form is is not left entirely, and then entered again, the entry still remains in the DataSource.
So essentially, the DataSource is not updating. (It's not running its select statement and repopulating until the whole page is reloaded). I've tried to use a page refresh and it doesn't seem to work, only going to a different page entirely and then coming back seems to update the DataSource.
How would I refresh the contents of the DataSource programmatically without having to recreate the entire DataSource itself?
You might want to add some code next time.
My best guess for now is that you're not doing anything OnPostback.
So I eventually resorted to doing what I did not want to do in the hopes that there would be a "Best-practice" way to do it (and one hopefully already built-in into DataSources), but it seems that was not to be.
All it took was to re-assign the DataSource's parameters to it again and then assign the DataSource back to the DropDownList, as if I were creating a new one.
I created the following method, which I then called at the end of my Delete button's event.
protected void DropDownList_Reload()
{
MyDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;
MyDataSource.SelectCommand = "SELECT * FROM [tblMyTable] WHERE [IsDeleted] <> 1";
cbxDropDownList.DataSourceID = "MyDataSource";
cbxDropDownList.DataTextField = "MyHeader";
cbxDropDownList.DataValueField = "MyHeader";
}

ASPX C# Form that automatically adds another row of fields If Fields have Values

So basically what i want to do is, automatically create a 2nd row of fields if the first initial row is filled out.
It does not need to be triggered by a button, necessarily. How would i do this?
You will probably need some JavaScript tot resolve this cliƫnt side. For instance a postback on the last cell modification.
Represent this as a Grid control that can be represented by a DataTable. Each row will be a DataRow. Bind the DataTable to this grid.
Do a postback for each of the individual fields in the row. Run a method to check if all fields are non-empty for each postback. If yes, add a new row. Rebind DataTable to the form.
Will this work?
Take a look at this Knockout.js video on MSDN.
The sample code is there too to add fields on the fly.
http://channel9.msdn.com/Events/MIX/MIX11/FRM08

GridView.Databind() not working with object list

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.

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();

how to insert row in grid view

i want to insert a new row to grid view while i click new button with out using the sqldatasource. and also how to edit, update, and delete rows from grid view. pls help me
[visual studio 2008
asp.net with c#]
thanks
thiru
What do you mean by "without using the SqlDataSource" ? How do you intend to propagate the inserted data to the data store, then ? You have not specified what other method you are using.
Anyway, the GridView does not inherently support insertion of records, but you can accomplish it by creating a FooterTemplate in which you create the fields for entry of new data. Additionally, provide a column to allow for Insert/Cancel buttons in the FooterTemplate.
Here's a good sample: How to easily insert row in GridView with SqlDataSource
Good gridview tutorial : ASP.NET Quickstart Tutorials
For adding new row you could try putting controls in footer row of the GridView.
Editing, updating and deleting require you to do two things:
implement those operations in the SqlDataSource
enable these operations in GridView
If you want to do it without the SQL datasource, then set DataTable as a GridView's datasource.
Good example on how to populate and add a row to datatable.

Categories

Resources