Custom update gridview and ObjectDataSource - c#

I have a gridview displaying data by using an Obejct Data Source which is then connected to a Business Layer.
The data shown/retrieved is from two tables with a join.
Is it possible to have a custom update method which, when the user clicks edit against the row, it will update only the one table out of the two that's required for the data shown (however if it can update the entire record that's fine too)? If yes how do i tie the ODS, along with my BL? Any info would be appreciated as my research so far has not given any examples of this.
To give an example one table is a user and the other table is results. The Results table is connected to the Users table by a UserID (foreign key) but the gridview shows a little more info when both tables are joined.

You can place a linkbutton inside a template field & give it CommandName="MyUpdate". You can then check for the commandName on rowCommand event of gridview & handle your custom update there.
if(e.CommandName =="MyUpdate")
{
// Perform your update
}

Related

How do I create a gridview with an empty column to allow user to enter a value which will then be used with the rest of the columns to update a table

How do I create a gridview with an empty column to allow user to enter a value which will then be used with the rest of the columns to update a table. I was hoping to allow user to go down a gridview change a number of values and upon button push have the new table displayed to review changes before submitting to a database. The gridview is dynamically bound from an ilist which makes it difficult as the page lifecycle disables me pulling the items out using .findcontrol. And Neither can I use viewstate("newtable") = ilist method as this will not account for the editable field. Help!!!
J
Firstly, your question is not clear if you put your code sample it will be more. if you work with entity framework for database op., datagridview already allow updates. yourentityobject.SaveChanges() is enough.

gridview click event that uses object data

I have a class (let's call it Object) that contains an ID and some other data. I want to display the IDs in a gridview. When the user clicks on an ID in the gridview table, I want the ability to add code that accesses the object of the ID and display the Object's details (it's ID and other stored data).
So far I've been able to store the data in a tabletable and set it as the gridview source, but I haven't been able to extend the click event feature.
Any ideas on how to implement this in C#?
The object used to load into the GridView is gone as soon as the page unloads. The contents are retained in viewstate, but the object is not there. What you need to do is query the data again using the selected ID. Or link the Grid to a DetailsView, which will query the data, as #Wahtever suggested in the comments.

How to update Database Table when multiple rows are edited in Gridview

I have inserted the screenshot of my Windows Form which pulls up information from a Product Table:
Now as you can see i have allowed the option to Modify and then Save the modifications in the Database Table. So the issue i am facing is in updating the data in the Database Table. Since the user can update more than 1 row at a time in the GridView, how do i update the Database table?
I can either do a row-wise update, or i can loop through the GridView and update my table and then use a Command Builder.
I want know how do i achieve the solution in both scenarios.
I would loop through the rows and pass the values to a parameter and then update in database. One thing, it is more work that doesn't need to be done. But another thing that helps is you know whatever cell they edit will be committed to the database.

How to edit the GridView and Update values if the GridView is user defined

I have a GridView which is completely user defined. The first column of the GridView is from the ListBox1. The headers are defined using ListBox2.
There fore I need to update rows of the GridView with some integer value and make sure the Gridview holds that value until a button is clicked.
Once the button is clicked I want to read each columns of the GridView that has values entered and create a table in the database with the headers names as columns and just replicate the GridView as table in database.
I have attached a screenshot of my GridView. Kindly help me fix this problem.
There are 2 key things to implement UPDATE logic in a GridView
In the GridView markup, you have to add an eventhandler for OnRowUpdating event
<asp:GridView ID="XXX" runat="Server" .....
OnRowUpdating="UpdateRecord" ..........>
Then you need to implement this "UpdateRecord" method referred above with your custom code which will basically retrieve the data from the grid row and update it to your datasource
You can refer to some sample code of how this can be achieved here
To point out again, in your case, since you want to dynamically create the table as well, you will need to additionally do that as well. Personally, i have no clue why you would go with such a design but in absence of any information, i assume you know best.

Update a data row inside an asp.net Data Grid View

I think it would be easier for you to understand my problem if I explain the scenario a bit.
What I am doing is developing a system to a doctor using asp.net c#. For the creation of the prescription, I add a check box list for the list of medicines and submit button below it. When the submit button clicked selected check box list items added to the sql server database and show a data grid with the selected medicines in the same page.
In the data grid there are two columns for medicine name and dosage.Medicine name is taken from the database.I need to give the user the ability to enter the dosage through that grid view. I need to use update command because the record is already there in the sql server.But empty value in the dosage column.I tried to use update command in the data source control. But I can't figure out how to give the WHERE clause because I can't specify the database row which the updating record relate to.
I would be glad to hear some help from you. All the comments are welcome.
You need to set a parameter(s), please check this example to understand how it works. I guess, that you've added in the database some column that is unique identifier.. some autoincrement int or guid.

Categories

Resources