C#中输入的数据

C#中输入的数据

本文介绍了丢失在dataview C#中输入的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Visual C#2008 制作一个应用程序,该应用程序从文本框获取数据并将其显示在 datagridview 以另一种形式使其输入数据库。
我使用 dataTable 发送数据,并且函数输入了数据而没有任何对称错误,但是当我为 datagridview调用另一个为空,数据库为空。当我复制主键时,它给出一个错误,指出无法复制主键 。

I am using Visual C# 2008 to make a application that takes the data from textboxes and displays it in datagridview in another form the conforming make it entered to the database.I send the data using dataTable with a function entered the data without any symentic error but when I call the other for the datagridview comes empty and the database comes empty. When I duplicate a primary key it gives an error stating "cannot duplicate primary key".

这是代码用于传输数据表

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

this is the code that I call the function in the other From

private void Cashagree_Load(object sender, EventArgs e) {
  dataGridView1.DataSource = ch.showout();
}

同一类中的第二个数据网格输入功能

the second datagrid entering function its in the same class

    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();

    }


推荐答案

正在解决通过将 DataTable dt 从第一份表格现金发送到第二份表格Cashagree 作为参数,通过调用以现金形式返回datagridview
的方法,我这样写:

In solve it by the sending the DataTable dt from the first Form cash to the second Form cashagree as a prameter by calling the method that return datagridview in cash form I wrote this:

  cashagree gc2 = cashagree(showout());

我以书面形式的同意书形式

in cashagree form I wrote this

  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;



      }

这篇关于丢失在dataview C#中输入的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 06:21