丢失数据视图中输入的数据C#

时间:2014-09-16 13:09:27

标签: c# forms datagridview

我正在使用Visual C# 2008制作一个应用程序,该应用程序从textboxes获取数据,并以另一种形式将其显示在datagridview中,并将其输入到数据库中。 我使用dataTable发送数据,函数输入数据时没有任何系统错误,但是当我调用另一个时,datagridview变为空,数据库变空。当我复制主键时,它会给出一个错误,指出"cannot duplicate primary key"

这是传递datatable

的函数的代码
public DataTable showout() {
    DataTable dtab = new DataTable();
    DataColumn dc1 = new DataColumn("رقم المتسلسل");
    DataColumn dc2 = new DataColumn("رقم الحساب");
    DataColumn dc3 = new DataColumn("أسم الحساب");

    dtab.Columns.Add(dc1);
    dtab.Columns.Add(dc2);
    dtab.Columns.Add(dc3);

   // Create an array for the values. 
   object[] newRow = new object[3];

    // Set the values of the array.
     string s = numb.Text;


     newRow[0] =numb.Text;
     newRow[1] = textBox5.Text;
     newRow[2] =note.Text;


     DataRow row;

     dtab.BeginLoadData();


     // Add the new row to the rows collection.
     row = dtab.LoadDataRow(newRow, true);

    return dtab;
}

这是我在其他From

中调用该函数的代码
private void Cashagree_Load(object sender, EventArgs e) {
  dataGridView1.DataSource = ch.showout();
}

第二个数据网格在同一个类中输入函数

    private void button1_Click(object sender, EventArgs e)
    {

        dataGridView1.Visible = true;
       dataGridView1.DataSource = showout();

        entering(true);
    }

这是进入数据库

   public void entering(bool bl)
      {try{
        if (bl)
        {

            using (SqlConnection connection = new SqlConnection(connectionString))
            {

                DateTime Date = DateTime.Today;


                SqlCommand cmd = new SqlCommand("INSERT INTO Accont(Account_ID,Account_Name,Owners,Curency,Curncytype,Depet,Credet_devet,Date,Note) VALUES (@AccountID, @AccountName, @Owner, @Curncy,@Curncytype,@Depet,@Cridetdevet,@Date,@Note)");
                cmd.CommandType = CommandType.Text;
                cmd.Connection = connection;
                cmd.Parameters.AddWithValue("@AccountID",numb.Text);
                cmd.Parameters.AddWithValue("@AccountName", comboBox1.SelectedText.ToString());
                cmd.Parameters.AddWithValue("@Owner", owner.Text);
                cmd.Parameters.AddWithValue("@Curncy", curency.Text);
                cmd.Parameters.AddWithValue("@Curncytype", curncyval.Text);
                cmd.Parameters.AddWithValue("@Depet", Depet.Text);
                cmd.Parameters.AddWithValue("@Cridetdevet", textBox5.Text);
                cmd.Parameters.AddWithValue("@Date", Date);
                cmd.Parameters.AddWithValue("@Note", note.Text);
                connection.Open();//Owner
                cmd.ExecuteNonQuery();}}
    }
        catch(Exception ee)
        {MessageBox.Show(ee.Message);}

符合另一种形式

   private void button1_Click_1(object sender, EventArgs e)
    {
            ch.entering(true);
            Close();

    }

2 个答案:

答案 0 :(得分:0)

您应该使用dtab.Rows.Add(datarow)方法,而不是使用dtab.LoadDataRow。 如何执行此操作的示例:

public DataTable showout()
{
  DataTable dtab = new DataTable();
  // More efficient way of adding the columns with types:
  dtab.Columns.Add("رقم المتسلسل", typeof(String));
  dtab.Columns.Add("رقم الحساب", typeof(String));
  dtab.Columns.Add("أسم الحساب", typeof(String));
  /*
  DataColumn dc1 = new DataColumn("رقم المتسلسل");
  DataColumn dc2 = new DataColumn("رقم الحساب");
  DataColumn dc3 = new DataColumn("أسم الحساب");

  dtab.Columns.Add(dc1);
  dtab.Columns.Add(dc2);
  dtab.Columns.Add(dc3);
  */

  // Create a new row using the .NewRow method
  DataRow datRow = dtab.NewRow();
  datRow["رقم المتسلسل"] = numb.Text;
  datRow["رقم الحساب"] = textBox5.Text;
  datRow["أسم الحساب"] = note.Text;

  // Add the new row to the DataTable
  dtab.Rows.Add(datRow);

 return dtab;
}

参考:

How to: Add Rows to a DataTable
Adding Data to a DataTable

答案 1 :(得分:0)

通过调用返回datagridview的方法,将DataTable dtfirst Form cash发送到second Form cashagree作为参数来解决此问题  以现金形式我写道:

  cashagree gc2 = cashagree(showout()); 

以cashagree形式我写了这个

  DataTable dt = new DstsTsble();
 public cashagree(DataTable d2){
   dt =d2;

 }
 and in the load of `cashagree_Load` I asign the datagridview datasoure 

   private void Cashagree_Load(object sender, EventArgs e)
    {
        if (dt.Rows[0].IsNull(dt.Columns[0]))
        {
            MessageBox.Show("There no primary key");
            Close();
        }
        dataGridView1.DataSource = dt;
        dataGridView1.Columns[7].Visible = false;// Iwantn't all the datatable so I diapple some Columns
        dataGridView1.Columns[5].Visible = false;
        dataGridView1.Columns[7].Visible = false;
        dataGridView1.Columns[6].Visible = false;
        dataGridView1.Columns[4].Visible = false;



      }