Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
In my form combobox , textbox and button are there
when i clicked the button combobox data and text box data has to display in the datagridview.
here is my code.
DataTable dt = new DataTable();
dt.Columns.Add("IEC Parameter Name");
dt.Columns.Add("Modbus Address");
dt.Columns.Add("Variable Type");
dt.Columns.Add("Variable Description");

DataRow dr = dt.NewRow();

dr["IEC Parameter Name"] = cmb_RelayName.Text + "/" + Cmb_LogicalNode.Text + "." + cmb_CommonDataClass.Text + "." + cmb_DataAttribute.Text; ;
dr["Modbus Address"] = tb_Modbus.Text;
dr["Variable Type"] = cmb_varaibleType.Text;
dr["Variable Description"] = textBox2.Text;
dt.Rows.Add(dr);
dataGridView1.DataSource = dt;
but my problem is:
i am displaying the selected data in datagridview .When i click the button again with out closing the form i want to display previous data on first row and newly selected data on the second row like etc.. on button click event . how can i do this . Please suggest me.
Posted
Updated 3-Nov-14 0:20am
v2

1 solution

Hi,

1. Declare Datatable as a global variable
2. Create datatabele columns in the Form Load event
3. Add a new Data row and Binding it to the grid.

Please let me know is this you requested...
C#
namespace WFDS
{
    public partial class Form1 : Form
    {
        DataTable dt = new DataTable();
        int i = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            i++;
            DataRow dr = dt.NewRow();

            dr["IEC Parameter Name"] = "Data" + i.ToString();
            dr["Modbus Address"] = "Data" + i.ToString();
            dr["Variable Type"] = "Data" + i.ToString();
            dr["Variable Description"] = "Data" + i.ToString();
            dt.Rows.Add(dr);
            dataGridView1.DataSource = dt;
        }






        private void Form1_Load(object sender, EventArgs e)
        {

            dt.Columns.Add("IEC Parameter Name");
            dt.Columns.Add("Modbus Address");
            dt.Columns.Add("Variable Type");
            dt.Columns.Add("Variable Description");


        }
    }
}
 
Share this answer
 
Comments
samyuktha.k 4-Nov-14 0:40am    
Thanks
Its working Fine.

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