How to insert multiple rows from a datagridview to sql server in c # using dataset

0

This is the Form in which I am working the idea is that you can select any checkbox and those you select are saved along with the data that are in the textbox above.

First of all, the datagridview is full

 public DataSet llenardataDV(string tabla)
    {
        DataSet DS;

        string cmd = string.Format("select IdRecepcionesPergamino, IdProveedor, IdContrato, Kilogramos from " + tabla);
        //DS = conexionazure.Ejecutaazure(cmd);
        DS = SQLLocal.conexionazure.Ejecutaazure(cmd);
        return DS;
    }

    private void TRASPASOS_Load(object sender, EventArgs e)
    {
        dataGridView1_BuscarProductor.DataSource = llenardataDV("RecepcionesPergaminos").Tables[0];
    }

The checkbox is done in this way

first I can not select with the checkbox and then I do not understand how I can insert the data of the row that is selected through the checkbox. I would appreciate your help, thank you very much

    
asked by Julio Vásquez Díaz 13.04.2018 в 04:01
source

2 answers

0

We are going to elaborate a response that can help you if you do not answer my comment. I'm going to assume that when you click on the check, what you do is select the row in question, and that once the selections are made, you'll hit the save button to save the data in a table.

If the function of the check is simply that of selecting a row you would not need that check, it would be enough to work with the selected rows and in the click event of the save button do something like this:

 private void btnSave_Click(object sender, EventArgs e)
    {
        foreach (var selectedRow in dataGridView1_BuscarProductor.SelectedRows)
        {
            //Codigo para salvar los datos.
        }   
    }

I hope I can help you, and if you give me more information I will try to give you more details.

a greeting

    
answered by 13.04.2018 в 09:59
0

Based on what I could understand about your question, I offer you an alternative solution:

Regarding this I can not select with the checkbox the first thing you should check is the following:

Your DataGridView must have the editing option enabled, also verify that the column you want to be editable in this case type checkbox has the property ReadOnly established in false :

Then, assuming you're going to save the data once you see the Botón Guardar :

DataSet dataset = new DataSet();

private void btn_guardar_Click(object sender, EventArgs e)
{
    dataset.Tables[0].Columns.Add("folioOrden", typeof(string));
    dataset.Tables[0].Columns.Add("tipoMovimiento", typeof(string));
    dataset.Tables[0].Columns.Add("producto", typeof(string));
    dataset.Tables[0].Columns.Add("observaciones", typeof(string));
    dataset.Tables[0].Columns.Add("pesoNeto", typeof(double)); //o decimal
    dataset.Tables[0].Columns.Add("idRecepcionesPerga", typeof(int));
    dataset.Tables[0].Columns.Add("idProveedor", typeof(int));
    dataset.Tables[0].Columns.Add("idContrato", typeof(int));
    dataset.Tables[0].Columns.Add("kilogramos", typeof(double));


    foreach (DataGridViewRow row in dataGridView1_BuscarProductor.Rows)
    {
        if (Convert.ToBoolean(dataGridView1_BuscarProductor.Rows[row.Index].Cells[0].Value) == true) //True (Seleccionado)
        {
            var filas = dataset.Tables[0].NewRow();

            //LLenando las filas 
            filas["folioOrden"] = tuTextBoxFolio.text;
            filas["tipoMovimiento"] = tuTextBoxTipoMov.text;
            filas["producto"] = tuTextBoxProducto.text;
            filas["observaciones"] = tuTextBoxObservaciones.text;
            filas["pesoNeto"] = Convert.ToDouble(tuTextBoxPesoNeto.text);
            filas["idRecepcionesPerga"] = Convert.ToInt16(dataGridView1_BuscarProductor.Rows[row.Index].Cells[1].Value);
            filas["idProveedor"] = Convert.ToInt16(dataGridView1_BuscarProductor.Rows[row.Index].Cells[2].Value);
            filas["idContrato"] = Convert.ToInt16(dataGridView1_BuscarProductor.Rows[row.Index].Cells[3].Value);
            filas["kilogramos"] = Convert.ToDouble(dataGridView1_BuscarProductor.Rows[row.Index].Cells[4].Value);

            //Agrega las filas al dataset.
            dataset.Tables[0].Rows.Add(filas);
        }
    }

    ///Luego cuando vallas a guardar los datos a tu base de datos 
    ///recorres el 'dataset' e inserta todos los datos que contenga en la Base de Datos..
    for (int i = 0; i < dataset.Tables[0].Rows.Count; i++)
    {
        //Insertas los valores a la BD.....
    }
}

I explain the operation:

  
  • 1st. A dataset is created and you add the corresponding columns to each textbox and column of your 'datagridview.

  •   
  • 2nd. You scroll the datagridview and for each selected row, you assign the values to dataset .

  •   
  • 3rd. You scroll the dataset and insert that data in your Database in the way you are doing, that depends on you.

  •   

I hope it helps you!

    
answered by 13.04.2018 в 16:08