ASP.NET GridView set with DataSet, can't edit rows - c#

I'm having the following problem:
What I have is a search function that runs a query on my database using a SqlDataAdapter.
I then use:
DataSet ds = new DataSet();
dataAdapter.Fill(ds, "table title");
gridView1.DataSource = ds;
gridView1.DataBind();
I originally got an error that I then had both a dataSource and dataSourceId, so I cleared the previously used ID. The code works, I can search the table and my query will return rows and update the table. The problem is that I want the data that it returns to be editable. I have my grid view editable, and before I run the query, I can edit the rows of the table. But after the search is run, and the gridView is filled with my DataSet, I'm unable to update the rows. If I click the edit button, it gives me an error saying the RowEditing event wasn't handled.
I looked into that event, and understand what's happening, if I were to program in the events for RowEditing/RowUpdating/RowUpdated etc. I could get it to work, but is there no better way to do this? Is there a simpler way to set my dataSource in C# and be able to maintain the editablity of the rows?
Thanks a lot!

I suppose part of the question would be would you want to? I guess you're looking for a SaveDataChanges() method which brings about a few design concerns. One is you have to remember that web pages are stateless so to an extent your web page post isn't aware of the data it's just pulled and bound to the UI. The grid view control in a round about way nicely gets the user to help you out here and click update on the row they've amended. With this you have your event handler and you can just focus on the data that's been changed and just send one UPDATE statement.

Related

Clearing the Gridview in c#

I am making my C# project and I want to clear the Gridview but the main purpose is after the Gridview is cleared, only a new data inserted should be seen however the old data is saved in my database.I was able to clear my Gridview but after inserting new data my old data is also shown in that gridview which should not be seen.Will be thankful for your suggestions.
I'd say that in most cases the DataGrid is bound to a DataSet. You could simply exchange the DataSet with a different one. Or maybe you modify the contents of the original DataSet, but in this case you would maybe need to cut the connection to the database if you don't want the data getting persisted...

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

Refreshing DataGridView?

I would like to provide a "Refresh" button on a form containing a DataGridView object bound to a MySQL database. I have gotten screens without columns populated from a query with calculated fields by "new"ing the MySQLCommand, MySQLDataAdapter, MySqlCommandBuilder and DataSet objects and rerunning the query and reloading the binding source to the DataGridView object.
For ones that have calculated columns, this does not work.
Surely, there must be some way to have a DataGridView object reload from the DB.
I am using SharpDevelop C# 5 with .NET framework 4.0, MySQL Community Server 5.3.33 and MySQL Data Connector/.NET V 6.7.4
Any leads much appreciated!
Jesse and Brian,
I am doing WinForms - sorry for not being clear. I have just worked out a solution. First, just executing what was run in the initial load is not enough. I found one must "new" the MySqlCommand, MySQLDataAdapter, MySqlCommandBuilder and DataSet objects, then run the data fetch as in the form load. Also, this cannot be called while doing anything that looks or smells like "editing" or it will throw an exception. I wanted my form to refresh just after edit, so I put a timer on the form and enabled it at the end of the RowValidated event and let the timer call the ReloadGrid routine. Also, if the user is on, say row 822, I found the focus went to row 0 and cell 0, so I save the current row and cell to a point and then set the focus back after the data refresh.
private void BindGrid(){
//retrieve data
//bind
DataGridView.DataSource = dataSource;
DataGridView.DataBind();
}
//call BindGrid() anytime you need to refresh data, for example in a button event click handler

Databinding, dataset with datarelations, and refreshing single DataTable from DB

In a nutshell: I have a DataSet with multiple DataTables inside, which are connected via multiple DataRelations. I've created a form using databinding on this DataSet, and you could say it is pretty complex.
The scenarios is next: the main purpose of the form is the insertion of a new record. For it to be inserted, various parent-child relations must be set. It all works fine with the databinding, but the problem is next: when user wants to enter a new "child" for one-of-those parent-child relations, new form opens, he enters it, yada yada yada, - and it works. Trouble is - how should I get that new data back to the original form, to appear in parent-child combobox?
One way I know of, is to refresh the entire form; but I am looking for a solution to refresh just the DataTable containing that data. I've tried getting a new DataTable with fresh data from DB, and merging it with DataTable in DataSet, but it doesn't work: "A child row has multiple parents" exception is thrown:
// basically just a Select * using dataadapter,
// nothing programmatically added
DataTable table = tableModule.GetPlainTable();
existingDataSet.Tables["tableName"].Merge(table);
The equivalent example of what I'm trying to do is to have a Order form, with Customer combobox on it. On the right of the combobox, there is a "add" button that opens a new "Add customer" form. Just to mention (maybe it is relevant), inside the DB, "Customers" table is in relation with the "Cities" table, and that data is linked in databindning on the Orders form as well...
EDIT: one solution I can think of is to "manually" loop throughout the new DataTable, and find all rows that are not in the original DataTable inside DataSet, but I'm not sure this would work either ...
Any help / suggestion / explanation / push in the right direction would be greatly appreciated :)
Well, i kind of solved the problem with manual row copy upon insertion in second form... I have created an event with object collection (row item array), and in first form registered a handler on that event, in which I manually add the new row via:
data.Tables["tableName"].Rows.Add(e.RowItems);
after which I call AcceptChanges() on that row to make its state Unchanged, rather than Added...
This solution, although quick-and-dirty, works. The problem is, I think there is a better, more "databinding" philosophy oriented one... :=)

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