我使用数据表保存数据网格视图数据并将其保存到另一个数据网格视图 [英] i saved the data grid view data using data table and save it to the another data grid view

查看:56
本文介绍了我使用数据表保存数据网格视图数据并将其保存到另一个数据网格视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据库如下;

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使用数据表将数据网格视图数据保存到另一个数据网格视图数据中。



i希望将数据网格视图数据保存到数据库中。



怎么做。请帮助我。


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.

推荐答案

我假设您正在使用winforms开发此应用程序。

您需要做的是将dataGridView中的每一行存储在您的表中,类似于下面,



你可以通过传递我希望你做的参数以非常有效的方式做到这一点,但重点是你有了这个想法用于将其存储在数据库中

I assume you are developing this application in winforms.
What you need to do is take every row from dataGridView store it in your table, something like below,
OR
you can do this in a very efficient way by passing parameters which i''d prefer you to do, but the important point is you got the idea for storing this in database
for (int i = 0; i < dataGridView.Rows.Count-1; i++)
            {
                string query = "insert into tableName(Date ,Session ,Course Faculty_ Code)" + "values('"+Convert.ToDateTime(dataGridView.Rows[i].Cells[0].Value.ToString())+"', '"+int.Parse(dataGridView.Rows[i].Cells[1].Value.ToString()) +"', '"+dataGridView.Rows[i].Cells[2].Value.ToString() +"', '"+dataGridView.Rows[i].Cells[3].Value.ToString()+"'";

System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(query,yourConnection);
cmd.ExecuteNonQuery();
}



好​​运


Good Luck


这篇关于我使用数据表保存数据网格视图数据并将其保存到另一个数据网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆