如何从文本框在非空网格视图中添加更多数据. [英] How to add more data in a non empty grid view from a text box....

查看:64
本文介绍了如何从文本框在非空网格视图中添加更多数据.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友,快乐洒红节

朋友,我有2个文本框,1个按钮和1个网格视图,

我已经使用以下代码从文本框(第一次网格视图为空)输入了一些记录到datagridview中..

Dear Friends Happy holi

friends i have 2 Text box, 1 button and 1 grid view ,

i have enter some record into datagridview from text box (first time grid view is empty)using the following code..

DataTable MyTable = new DataTable();
                DataColumn values = new DataColumn("Column1");
                DataColumn values1 = new DataColumn("Column2");
               
                MyTable.Columns.Add(values);
                MyTable.Columns.Add(values1);
               

                DataRow rd = MyTable.NewRow();
                rd[0] = textBox1.Text;
                rd[1] = textBox2.Text;
                MyTable.Rows.Add(rd);
                dataGridView1.DataSource = MyTable;


但是,当我尝试添加更多记录时,例如单击确定"按钮g,然后在网格视图上添加记录,并且文本框为空,然后在文本中输入一些内容,然后单击确定"按钮,然后将新记录追加到旧记录下.但是当我使用此代码时,数据将被覆盖.

我想在网格视图不为空时每次都向网格视图中添加新行...

请通过在此处提供代码来提供帮助.在此先感谢....


but when i am trying to add more record like we click on ok buttong then record add on grid view and text box are empty then enter some thing in to text and click on ok button then a new record append down of old record. but when i am using this code data is getting override .

i want to append a new row every time into a grid view when grid view is not empty...

please help by giving code here . thanks in advance....

推荐答案

您需要先将列添加到表中,然后才能添加行.
you need to add the columns to the table before you can add rows.
        DataTable dt = new DataTable();
        DataColumn dc = new DataColumn("name");
        dt.Columns.Add(dc);
        dc = new DataColumn("field");
        dt.Columns.Add(dc);
//Now your table has defined columns
// you can add rows...
 
        DataRow dr = dt.NewRow();
        dr["Name"] = TextBox1.Text;
        dr["field"] = TextBox2.Text;
 
        GridView1.DataSource = dt;
        GridView1.DataBind();



希望对您有帮助



Hope this will help you


这篇关于如何从文本框在非空网格视图中添加更多数据.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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