Thursday 9 October 2014

C# Step by Step How to dynamically get values from textboxes into gridview



First Step Open Visual studio 

Click on New Project


Now Menu visible Select option Visual C# just like in Picture below

now  select windows form from visual C# windows and name project and save it
now project will be loaded and open the project to design and code
now their is form visible you can now start working


now you see label in toolbox where it can be dragged to create identification of something on interface to use it

now we have dragged
now to change the name of label1 to ID so we write click of mouse and go to its properties which will be visible on write side to change the text name just like below



Another label drag and dropped and change the text name to Name


Now we need textBoxes to get values from user

To identify user to use it we have to change its accessability name value to use in code by going to its properties




Now its accessable for id values now applying same above procedure we have to show textbox for name in change accessibility name is txtName

now to save the data we have to use button on interace where user click and these values in boxes will be stored by drag button from toolbox and drop on form just like that below and change textname from button to Save by using properties

 changing accessibility name of the button to accessable in code





Now after that we need some visible excel like sheet view to visible all the records on button click to user in less time so for show data we use gridview and then drag from toolbox and drop it on form
just like below


now we do code by double click on interface where save button is displaying click on it you will se code view where you can do code



after coding with no error debug and result will be displayed you give input and result dropped and showed in gridview just like that



make sure your project name is same at that line else you will get error to run code


write code as below with different textboxes are used on interface like txtID and txtName dont confuse below is mine written code with different names so dont write as exact just go to your button , textbox property and copy accessibility name use them in code you will avoid errors

Hope you will like this

Code


Form2.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MyProjectToGetGridDynamically
{
    public partial class Form2 : Form
    {
        DataTable dt;
        DataRow dr;
        public Form2()
        {
            InitializeComponent();
            dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("name");

        }

        private void button1_Click(object sender, EventArgs e)
        {
            dr = dt.NewRow();
            dr[0] = textId.Text;
            dr[1] = textName.Text;
            dt.Rows.Add(dr);
            dataGridView1.DataSource = dt;
        }
    }
}

1 comment: