将C#datagridtextboxcolumn单元格值保存到Sql数据库 [英] Saving C# datagridtextboxcolumn cell value to Sql database

查看:72
本文介绍了将C#datagridtextboxcolumn单元格值保存到Sql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
希望您能帮助我完成此编码.
我有一个sql数据库和一个带有datagridview的C#应用​​程序.
我使所有代码都能正常工作,以便数据填充datagridview,一旦填充了datagridview,我需要将单元格的值保存在colAddress列中,该列包含地址.
我与sql数据库建立了数据连接,并且数据库包含到运行我的应用程序按钮单击事件时添加到的列.
到目前为止,它将输入记录,但不会记录每个单元格的实际值.相反,它将正确显示输入日期时间戳,然后将地址"列输入为False,而不是例如单元格中的实际值.大街123号.因此,数据库中的记录显示为:

Hi all,
Hoping you can help me finish this coding.
I have a sql database and a C# application with a datagridview.
I have the coding all working correctly for data to populate the datagridview and once I have the populated datagridview I need to save the value of the cells in the colAddress column, which contains addresses.
I have dataconnection to the sql database and the database contains to columns which are added to when I run my application button click event.
So far it will enter the records, but does not record the actual value of each cell. Instead it will display enter the datetime stamp correctly then enter the Address column as False, instead of the actual value in the cell eg. 123 Main Street,. So the records in my database display as:

colAddress     createdDate
False         2012-02-24 00:00:00.000
False         2012-02-24 00:00:00.000
False         2012-02-24 00:00:00.000
False         2012-02-24 00:00:00.000
False         2012-02-24 00:00:00.000
False         2012-02-24 00:00:00.000



到目前为止,这是我的编码,如何更改此代码以获得我想要的结果?



Here is my coding so far, how can I alter this code to get the result that I am after?

private void button5_Click(object sender, EventArgs e)
        {
            if (dgv.Rows.Count <= 0)
            {
                MessageBox.Show("No List to save.", "Empty Selection", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            if (dgv.Rows.Count >= 1)
            {
                DialogResult dr = MessageBox.Show("Are you sure you want to save the data", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (dr == DialogResult.Yes)
                {
                        List<DataGridViewRow> rows_w_checked_column = new List<DataGridViewRow>();
                        
                        foreach (DataGridViewRow row in dgv.Rows)
                        {
                            
                            if (Convert.ToBoolean(row.Cells[colPrint.Name].Value) == true)
                            {
                                rows_w_checked_column.Add(row);

                                int x;
                                DateTime createdDate;
                                createdDate = Convert.ToDateTime(DateTime.Now.ToString("dd-MMM-yyyy"));

                                
                                // Database connection
                                SqlConnection frsCon = new SqlConnection();
                                frsCon.ConnectionString = "Data Source=DEVSERVER;Initial Catalog=FRS;Integrated Security=True";
                                SqlDataAdapter frsApt = new SqlDataAdapter();
                                SqlCommandBuilder frsCmdBlr = new SqlCommandBuilder();
                                DataTable frsDGV = new DataTable();
                                BindingSource frsBS = new BindingSource();

                                SqlCommand frsCommand = new SqlCommand("INSERT INTO tblAddress (colAddress, createdDate) VALUES (@colAddress, @createdDate)", frsCon);
                                frsCommand.Parameters.Add("@colAddress", SqlDbType.VarChar).Value = colAddress.Selected;
                                frsCommand.Parameters.Add("@createdDate", SqlDbType.DateTime).Value = createdDate;

                                frsCon.Open();
                                x = frsCommand.ExecuteNonQuery();
                                frsCon.Close();
                            }
                            
                        }
                        
                }
            }

推荐答案

萨斯

这行是错误的:

Hi Saas

This line is wrong:

frsCommand.Parameters.Add("@colAddress", SqlDbType.VarChar).Value = colAddress.Selected;



这就是为什么要返回布尔值的原因.您需要将colAddress.Selected替换为所选单元格内的值.



This is why you are getting a boolean value returned. You need to replace the colAddress.Selected with value inside the selected cell.


尝试此操作

Try this

frsCommand.Parameters.Add("@colAddress", SqlDbType.VarChar).Value = colAddress.SelectedCell.Value;


这篇关于将C#datagridtextboxcolumn单元格值保存到Sql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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