Я знаю, что на этот вопрос уже был дан ответ, но вот код для прохождения цикла через GridView для получения данных и их сохранения в базе данных:
Использование библиотек:
- используя System.Data;
- using System.Data.SqlClient;
- с использованием System.Web.Configuration;
- с использованием System.Data.Odbc;
Код сзади:
// 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's a string variable that have the Query or SQL Commands.
SqlCommand cmd = new SqlCommand(DataBaseQuery, conexion);
//this case it's 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's 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's 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();
}
}
Конечно, вы должны настроить код для вашего конкретного случая, но это очень легко,В этом коде у вас есть пример для получения значений для других объектов, таких как labes, textbox и dropdownlist в gridview.
Я много страдал, чтобы запустить этот код (я не очень хорошо программирую), но ярад помочь.
ПРИМЕЧАНИЕ. Для подсчета столбцов сетки необходимо начинать с нуля.ПРИМЕЧАНИЕ 2: Кстати, извините за мой плохой английский ... Это не мой родной язык.