将GridView数据更新为SQL [英] Update GridView Data to SQL

查看:88
本文介绍了将GridView数据更新为SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,

我正在使用C #Windows Application.I我正在使用for循环Condition.Iow将数据显示到网格视图中,我想将数据(在datagridview中显示)更新为SQL数据库......但它没有显示新添加的数据...

这里是我的示例代码在网格中显示数据....



Hi Friends,
I am using C# Windows Application.I am displaying SQL data into grid view using for loop Condition.Now, I want to update data(which is display in datagridview) into SQL Database...But it didn't display the newly added data...
here is my sample code Display data in Grid....

comm = "select Name,Rate,Qty,TotalAmount from SALESENTRY where PENo = '" + txt_VocherNo.Text + "'";

                SqlCommand sqlcom = new SqlCommand(comm, sqlcon);
                SqlDataReader read = sqlcom.ExecuteReader();
                if (read.HasRows)
                {
                    int k = 0;
                    while (read.Read())
                    {
                        dgv_Entry.Rows[k].Cells["ColumnName"].Value = read.GetString(0);
                        dgv_Entry.Rows[k].Cells["ColumnRate"].Value = read.GetString(1);
                        dgv_Entry.Rows[k].Cells["ColumnRQty"].Value = read.GetString(2);
                        dgv_Entry.Rows[k].Cells["ColumnTotal"].Value = Total;
                        k++;
                    }
                }
                sqlcon.Close();





及其MY Update代码..





And its MY Update Code..

if (btn_save.Text.Trim() == "&Update")
                        {                                                         
               for (int no = 0; no <= dgv_Entry.Rows.Count - 1; no++)
                {
                    if (dgv_Entry.Rows[no].Cells["ColumnName"].Value.ToString() == "" || dgv_Entry.Rows[no].Cells["ColumnName"].Value.ToString() == null)
                    { break; }
                    else
                    {
                        VNo = txt_VocherNo.Text;                        
                        Name = dgv_Entry.Rows[no].Cells["ColumnName"].Value.ToString();
                       SRate = dgv_Entry.Rows[no].Cells["ColumnRate"].Value.ToString();
                       RQty = dgv_Entry.Rows[no].Cells["ColumnRQty"].Value.ToString();
                       Total = dgv_Entry.Rows[no].Cells["ColumnTotal"].Value.ToString();
                      Cmmd = "Update SALESENTRY Set Name = '" + Name + "',SRate ='" + Rate + "',RQty='" + Qty + "',Total='" + TotalAmount + "' Where Vno = '"+Vno+"'
            DbCon.SetDataBase(Cmmd);
                    }
                }
                                MessageBox.Show("Record Updated", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);





现在如何更新新添加的记录旧的

例如。,我已经有4条记录了,现在我想用更新按钮添加一条记录。如何更新?..

请建议...



Now How can i update Newly Added record Instead of Old
For Example.,I have Already 4 Records, now i want to Add one More Record using Update Button..How can i update?..
Please Suggest...

推荐答案

嘿那里,



看到我得到的你想要插入&更新同时执行(在1次点击事件中),

所以我将解释逻辑方法。如下所示



1)在INSERT或UPDATE检查是否存在行的主要(INDEX)

之前(在网格视图中) )



如果(您的行ID存在< using select =>)

执行UPDATE_SQL_QUERY

否则

执行INSERT_SQL_QUERY



---------

Students_Table示例
检查是否(Students_Table.Roll存在)

更新名称='吉姆随身携带'

否则

插入学生姓名新的'汤姆克鲁斯'
Hey there,

See i get that you want Insert & Update to performed at the same time(on 1 click event),
So i will just explain the logical ways to do it. as below

1) Before you INSERT or UPDATE check for the existence of Primary (INDEX) of the row
(in your grid view)

If (Your row ID exists<using select=""> )
Execute UPDATE_SQL_QUERY
else
Execute INSERT_SQL_QUERY

---------
Example for Students_Table
Check if (Students_Table.Roll is present)
Update Name = 'Jim carry'
Else
Insert Student with name new 'Tom Cruise'


这篇关于将GridView数据更新为SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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