3
Answers

save the data grid view data in to data base.

narasiman rao

narasiman rao

12y
2.7k
1
Database as follows;

 Date         datetime
 Session      int
 Course       varchar(20)
Faculty_ Code varchar(20)


 but i want save the data grid view data in to the data base.

private void BtnSave_Click(object sender, EventArgs e)
        {
            Save Details();
        }


 private void SaveDetails()
        {
            try
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("date", typeof(string));
                dt.Columns.Add("sess", typeof(int));
                dt.Columns.Add("crs", typeof(string));
                dt.Columns.Add("Fac", typeof(string));
                for (int i = 0; i < datagridView.RowCount; i++)
                {
                    for (int j = 2; j < datagridView.ColumnCount; j++)
                    {
                        if (datagridView[j, i].Value != null)
                  
                        {
dt.Rows.Add(datagridView[0, i].Value.ToString(), datagridView[1, i].Value.ToString(), datagridView[j, i].Value.ToString(), datagridView.Columns[j].HeaderText.ToString());
                        }
                    }
                }
                dataGridView1.DataSource = dt;
            }
            catch (Exception Ex1)
            {
                MessageBox.Show(Ex1.ToString(), "Error", MessageBoxButtons.OK);
            }
        }


i save the data grid view data into the another data grid view data using data table.

i want to save the data grid view data in to the data base.

how to do.please help me.
Answers (3)
1
Dorababu Meka

Dorababu Meka

NA 10.4k 1.2m 12y
What is your data in datagridview?
0
Dorababu Meka

Dorababu Meka

NA 10.4k 1.2m 12y
You are not showing what your data table values are as per you given try this code if you are filling datagridview with datatable try this code

Form_load code

 private void Form3_Load(object sender, EventArgs e)
        {
            DataTable table = new DataTable();
            table.Columns.Add("sess", typeof(int));
            table.Columns.Add("crs", typeof(string));
            table.Columns.Add("Fac", typeof(string));
            table.Columns.Add("date", typeof(DateTime));
            table.Rows.Add(25, "C#", "Dorababu", DateTime.Now);

            dataGridView1.AutoGenerateColumns = true;
            dataGridView1.DataSource = table;
        }

Your button code

private void button1_Click(object sender, EventArgs e)
        {
            DateTime dt = new DateTime();
            int id = 0;
            string course = string.Empty;
            string Faccode = string.Empty;

            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                id = Convert.ToInt16(dataGridView1.Rows[i].Cells["sess"].Value.ToString());
                dt = Convert.ToDateTime(dataGridView1.Rows[i].Cells["date"].Value.ToString());
                course = dataGridView1.Rows[i].Cells["crs"].Value.ToString();
                Faccode = dataGridView1.Rows[i].Cells["Fac"].Value.ToString();
                  
                // Your database code for insert the value to database  
            }
        }
0
Neha Sharma

Neha Sharma

NA 5.6k 2.5m 12y
Hi,

Please go through with the following url :

http://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html

hope it ll help you.

Thanks.