簡體   English   中英

如何將文本框中包含的數據傳輸到datagridview?

[英]How to transfer data contained in a textbox to a datagridview?

我在Windows窗體應用程序中有4個文本框,還有一個名為“添加”的gridview和按鈕。 我想要的是,在4個文本框中輸入的數據應該轉到datagridview的同一行中的四個不同列,因為我單擊Add按鈕。 如果我清除文本框,再次填充它們並再次單擊“添加”按鈕,則數據應該轉到gridview的第2行。

在您的應用中的某個位置創建類似以下的類

public class MyClass
{
    public string field1 { get; set; }
    public string field2 { get; set; }
    public string field3 { get; set; }
    public string field4 { get; set; }
}

在你的form.cs里面寫這個,

public static List<MyClass> lst = new List<MyClass>();

在添加按鈕的單擊事件中執行此操作

private void btnAdd_Click(object sender, EventArgs e)
        {
            MyClass obj = new MyClass();
            obj.field1 = txt1.Text.Trim();
            obj.field2 = txt2.Text.Trim();
            obj.field3 = txt3.Text.Trim();
            obj.field4 = txt4.Text.Trim();

            lst.Add(obj);
            dataGridView1.DataSource = lst;
        }

要遵循的步驟:

1)在綁定DataGrid視圖時,將DataSource保存在Viewstate變量中

2)為Add按鈕定義Click事件在click事件中,調用具有數據源的Viewstate Variable,將其轉換為DataTable。 創建該數據表的新行,將值分配給新行的單元格。 將此新行添加到Datatable,再次在ViewSate中保存dataTable,將數據源綁定到DataGrid

這就是全部〜

首先感謝SHAKIR SHABBIR先生,但我認為Windows窗體應用程序中沒有viewstate ...

我建議使用靜態集合類型......

當您單擊添加按鈕創建類的對象,使用文本框設置其所有值,在網格中插入行並保存所有行數據(每個類的對象)時,您可以為您的條目形式定義一個類,並為文本框定義一個變量列表中的gridview行)

您還可以使用靜態數據表...您必須為每個文本字段創建列...將行添加到數據表中...選擇網格的數據源...

如果您需要進一步的幫助,我總是在這里幫助您..

使用此代碼:

 namespace WindowsFormsApplication1
 {
     public partial class Form1 : Form
    {
        DataSet ds = new DataSet();
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Dataset;Integrated Security=True");
        SqlDataAdapter ds1 = new SqlDataAdapter();

        BindingSource bd = new BindingSource();

        public Form1()
        {
            InitializeComponent();
        }

       private void btnAdd_Click(object sender, EventArgs e)
      {
        ds1.InsertCommand = new SqlCommand("INSERT INTO Employee VALUES(@FirstName,@LastName)", con);
        ds1.InsertCommand.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = txtFirstName.Text;
        ds1.InsertCommand.Parameters.Add("@LastName", SqlDbType.VarChar).Value = txtLastName.Text;

        con.Open();
        ds1.InsertCommand.ExecuteNonQuery();
        con.Close();
    }

    private void btndisplay_Click(object sender, EventArgs e)
    {
        ds1.SelectCommand = new SqlCommand("Select * from Employee", con);
        ds.Clear();
        ds1.Fill(ds);

        dataGridView1.DataSource = ds.Tables[0];

        bd.DataSource = ds.Tables[0];

        //txtFirstName.DataBindings.Add("Text", bd, "FirstName");
        //txtLastName.DataBindings.Add("Text", bd, "LastName");

    }

    private void btnPervious_Click(object sender, EventArgs e)
    {
        bd.MovePrevious();
        update();
        records();
    }

    private void btnNext_Click(object sender, EventArgs e)
    {
        bd.MoveNext();
        update();
        records();
    }

    private void btnFirst_Click(object sender, EventArgs e)
    {
        bd.MoveFirst();
        update();
        records();
    }

    private void btnLast_Click(object sender, EventArgs e)
    {
        bd.MoveLast();
        update();
        records();
    }

    private void update()
    {
        dataGridView1.ClearSelection();
        dataGridView1.Rows[bd.Position].Selected = true;
        records();
    }
    private void records()
    {
        label1.Text = "records" + bd.Position + " of " + (bd.Count - 1);

    }

別忘了標記這個答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM