Click here to Skip to main content
15,914,384 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I created on windows form with following controls.

TextBox - txtName
Button - btnAdd
DataGridView - gvTotalNames

When I click "btnAdd" button the txtName text should be inserted in to grid view.

Again entered some data into the txtName one more record should be displayed in to grid view.

How can I achieve this?

I can see the result like

Name

aaaa
bbbb
cccc
dddd
Posted
Updated 27-May-13 22:17pm
v2
Comments
Maciej Los 28-May-13 4:30am    
Which programming language: C++, C#, VB.NET, any?
Change the TAG. Replace ADO.NET with WinForms and your programming language. Use "Improve question" widget.

if your project is in vb.net then use this code else if C# then convert this code to C#
VB
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        gvTotalNames.Rows.Add()
        gvTotalNames.Item(0, gvTotalNames.Rows.Count - 1).Value = txtName.Text
        txtName.Text = ""
    End Sub
 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        gvTotalNames.AllowUserToAddRows = False
        gvTotalNames.Rows.Clear()       
    End Sub
 
Share this answer
 
C#
private DataTable dt;
private void Form1_Load(object sender, EventArgs e)
{

    dt = new DataTable("TotalNames");
    DataColumn dc = new DataColumn("Names");
    dt.Columns.Add(dc);
    gvTotalNames.DataSource = dt;
}

private void btnAdd_Click(object sender, EventArgs e)
{
    DataRow dr = dt.NewRow();
    dr["Names"] = txtName.Text;
    dt.Rows.Add(dr);
}
 
Share this answer
 
Comments
rajugknr 28-May-13 5:16am    
Thank you very much
Because you did not provide enough details, i'll show you a ways to achieve that...

You can do it in many ways, for example:
1) using DataTable[^],
2) using List(T)[^] generic class,
3) using ArrayList[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900