ASP GridView All rows in edit mode

On my ASP.NET page, I use GridView to view data (Products and their prices). Currently, users can edit data (prices) line by line. (Click โ†’ "Change", change the values, then "Update"). This is ROW by ROW. Is it possible to open all rows in edit mode and use one button (for example, Send) to update all data once?

+6
source share
3 answers

If you do not need a read-only mode, in this case you can put the input fields (text field, drop-down list, etc.) in the ItemTEmplate section and associate them with existing data.

Then place the submit button at the top / bottom of the GridView icon and the Click button and process the data of the GridView element and loop the data of the GridView element and save the entire database.

I will send a block of code if you need. Thank you for your time.

+5
source

And you will have better control over what you are doing, using listview instead of gridview.

My best practice is to use listview and a custom user control for such problems.

If you fill out your list using your custom control, it will be easy for you to control your save method, you just need to iterate over the list items, find the control and call the Save () method for each item.

0
source

I know that this question has already been answered, but here is the code for the loop through receiving the GridView data and storing it in the database:

Using libraries:

  • using System.Data;
  • using System.Data.SqlClient;
  • using System.Web.Configuration;
  • using System.Data.Odbc;

Code for:

 // this is a variable that have the Query or SQL Commands. string DataBaseQuery = "UPDATE [table] SET [variable2] = @variable2, [variable3] = @variable3) WHERE [variable1] = @variable1"; //Click Event from a LinkButton. protected void LinkButton1_Click(object sender, EventArgs e) { //"ConnectionString" its the string connection for your DataBase (often get from the WebConfig File or a DataSource element. using (SqlConnection connection = new SqlConnection(ConnectionString)) { //this is for open the database using the string connection. connection.Open(); //this is the algorithm for going through the entire GridView. for (int i = 0; i < GridView1.Rows.Count; i++) { //"DataBaseQuery" it a string variable that have the Query or SQL Commands. SqlCommand cmd = new SqlCommand(DataBaseQuery, conexion); //this case it for obtain the text variable of the first column of the gridview (in my case it was the ID of the register). cmd.Parameters.AddWithValue("@variable1", ((Label)GridView1.Rows[i].Cells[0].FindControl("Label1")).Text.ToString()); //this case it for obtain the selected value of a DropDownList that were in the 14 th column) cmd.Parameters.AddWithValue("@variable2", ((DropDownList)GridView1.Rows[i].Cells[15].FindControl("DropDownlist2")).SelectedValue.ToString()); //this command it for obtain the text of a textbox that is in the 15 th column of the gridview. cmd.Parameters.AddWithValue("@variable3", ((TextBox)GridView1.Rows[i].Cells[16].FindControl("TextBox17")).Text.ToString()); cmd.ExecuteNonQuery(); } //after going through all the gridview you have to close the connection to the DataBase. connection.Close(); } } 

Of course, you need to configure the code for a specific case, but it is very simple. In this code, you have an example to get values โ€‹โ€‹for other objects like labes, textbox and dropdownlist in gridview.

I suffered a lot to run this code (I'm not very good at programming), but I am happy to help.

NOTE. To count gridview columns you must start from scratch. NOTE2: Sorry for my poor English, by the way ... This is not my language of nature.

0
source

Source: https://habr.com/ru/post/903444/


All Articles