本文介绍了在运行时在gridview中添加值并将其保存到数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





i需要在运行时在我的网格视图中添加值并将其保存到数据库。我尝试了各种各样的事情,比如创建一个函数和调用和插入值但在运行时这不起作用。我是这个领域的新手,不太了解这个..请帮助。



我尝试过:



Hi,

i need to add values in my Grid-View at Run time and save it to the database.i tried various things like by creating a function and calling and inserting values but at run-time this Does not work. i am novice in this field and Don't know much about this ..please help.

What I have tried:

public void Fill_grid()
     {
         SqlCommand cmd = con.CreateCommand();
         cmd.CommandType = CommandType.Text;
        cmd.CommandText = "select * from DBTable";
         cmd.ExecuteNonQuery();
         DataTable dt = new DataTable();
         SqlDataAdapter da = new SqlDataAdapter(cmd);
         da.Fill(dt);
         dataGridView1.DataSource = dt;

     }

     private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
     {
         id = dataGridView1.Rows[e.RowIndex].Cells["id"].Value.ToString();
         if (id == "")
         {
             id1 = 0;
         }
         else
         {
             id1 = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["id"].Value.ToString());
         }

         if (id1 == 0)
         {
             SqlCommand cmd = con.CreateCommand();
             cmd.CommandType = CommandType.Text;
             cmd.CommandText = "Insert into DBTable values('" + dataGridView1.Rows[e.RowIndex].Cells["FileName"].Value.ToString() + "','" + dataGridView1.Rows[e.RowIndex].Cells["Sequence"].Value.ToString() + "','" + dataGridView1.Rows[e.RowIndex].Cells["Delay"].Value.ToString() + "')";
             cmd.ExecuteNonQuery();
             Fill_grid();
         }

         else
         {
             SqlCommand cmd = con.CreateCommand();
             cmd.CommandType = CommandType.Text;
             cmd.CommandText = "update DBTable set Filename='" + dataGridView1.Rows[e.RowIndex].Cells["Filename"].Value.ToString() + "',Sequence='" + dataGridView1.Rows[e.RowIndex].Cells["Sequence"].Value.ToString() + "',Delay='" + dataGridView1.Rows[e.RowIndex].Cells["Delay"].Value.ToString() + "' where id=" + id1 + "";
             cmd.ExecuteNonQuery();
             Fill_grid();

         }

     }

推荐答案


这篇关于在运行时在gridview中添加值并将其保存到数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 04:20