8 Replies - 44761 Views - Last Post: 02 March 2011 - 05:33 AM Rate Topic: -----

#1 monica hinge  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-February 11

add data from textbox into datagridview

Posted 23 February 2011 - 04:24 AM

DataRow rd;
        DataTable MyTable = new DataTable();
        private void button1_Click(object sender, EventArgs e)
        {
           
            rd["Column1"] = textBox1.Text;
            rd["Column2"] = textBox2.Text;
            rd["Column3"] = textBox3.Text;
           // MyTable.Rows.Add(rd);

        }


i tried a lot to add data from text box into datagridview on button click..
i m new to c# and really want help from you...is these is the right one bcoz..its show error..
Thanks You in Advance..

This post has been edited by insertAlias: 24 February 2011 - 08:00 AM


Is This A Good Question/Topic? 0
  • +

Replies To: add data from textbox into datagridview

#2 Vishu Sukhdev  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 150
  • Joined: 19-February 10

Re: add data from textbox into datagridview

Posted 23 February 2011 - 05:01 AM

Hi,
suppose you have DataGridView for example dgv
 private void GridDesign()
        {
            dgv.Columns.Add("srno", "SrNo");
            dgv.Columns.Add("bookingno", "Booking No");
            dgv.Columns.Add("bookingdate", "Booking Date");

            dgv.Columns[0].Width = 50;
            dgv.Columns[1].Width = 100;
            dgv.Columns[2].Width = 100;
           
                    }



call method on Page_Load()

and then write code on button_click()

private void button1_Click(object sender, EventArgs e)
{
 dgv.Rows.Add(textBox1.text,textBox2.text,textBox3.text);

}


Was This Post Helpful? 0
  • +
  • -

#3 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 641
  • Joined: 07-October 08

Re: add data from textbox into datagridview

Posted 23 February 2011 - 05:13 AM

Your code doe's not work for a variety of reasons. You are creating a DataTable instead of referenceing your DataGridView, you never assign rd so it will be null. You are trying to add a DataRow when you should be trying to add a DataGridViewRow, and you have commented out the line where you would actually add the row. This code should work...
// create new row
DataGridViewRow row = new DataGridViewRow();

// create cells
row.CreateCells(this.dataGridView1, textBox1.Text, textBox2.Text, textBox3.Text);

// add to data grid view
this.dataGridView1.Rows.Add(row);



EDIT: Sorry Vishu Sukhdev you beat me to it :)
Was This Post Helpful? 0
  • +
  • -

#4 Guest_monica hinge*


Reputation:

Re: add data from textbox into datagridview

Posted 23 February 2011 - 06:27 AM

View Postragingben, on 23 February 2011 - 05:13 AM, said:

Your code doe's not work for a variety of reasons. You are creating a DataTable instead of referenceing your DataGridView, you never assign rd so it will be null. You are trying to add a DataRow when you should be trying to add a DataGridViewRow, and you have commented out the line where you would actually add the row. This code should work...
// create new row
DataGridViewRow row = new DataGridViewRow();

// create cells
row.CreateCells(this.dataGridView1, textBox1.Text, textBox2.Text, textBox3.Text);

// add to data grid view
this.dataGridView1.Rows.Add(row);



EDIT: Sorry Vishu Sukhdev you beat me to it :)

Was This Post Helpful? 0

#5 Guest_monica hinge*


Reputation:

Re: add data from textbox into datagridview

Posted 23 February 2011 - 06:31 AM

thanks sir...i hyv tried a code like this too...plz will u give me some sort of suggestion to improve the code boz when i add other it dont work..

            DataTable MyTable = new DataTable();
           // DataSet ds = new DataSet();
           // ds.Tables.Add("myTable");
            DataColumn values = new DataColumn("Column1");
            DataColumn values1 = new DataColumn("Column2");
            DataColumn values2 = new DataColumn("Column3");
            MyTable.Columns.Add(values);
            MyTable.Columns.Add(values1);
            MyTable.Columns.Add(values2); 
            DataRow rd = MyTable.NewRow();
            rd[0] = textBox1.Text;
            rd[1] = textBox2.Text;
            rd[2] = textBox3.Text;
            MyTable.Rows.Add(rd);
           
            
           
           
            //ds.Tables["myTable"].Rows.Add(rd);
            dataGridView1.DataSource = MyTable;
          
            
         

        }
    }

thanks for helping sir...

This post has been edited by insertAlias: 24 February 2011 - 08:01 AM

Was This Post Helpful? 0

#6 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 641
  • Joined: 07-October 08

Re: add data from textbox into datagridview

Posted 23 February 2011 - 07:14 AM

I have modified your code a bit so that it works, heres the full code

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// Get or set the data table
        /// </summary>
        private DataTable myTable = new DataTable();

        public Form1()
        {
            InitializeComponent();

            // initialise
            this.initialiseTable(this.myTable);

            // set source
            this.dataGridView1.DataSource = this.myTable;
        }

        /// <summary>
        /// Initialise a table
        /// </summary>
        /// <param name="table">The table to initialise</param>
        private void initialiseTable(DataTable table)
        {
            // add all columns
            table.Columns.Add(new DataColumn("Column1"));
            table.Columns.Add(new DataColumn("Column2"));
            table.Columns.Add(new DataColumn("Column3"));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // create a row
            DataRow rd = this.myTable.NewRow();

            // set values
            rd[0] = textBox1.Text;
            rd[1] = textBox2.Text;
            rd[2] = textBox3.Text;
            
            // add row to table 
            this.myTable.Rows.Add(rd);
        }
    }
}



Basically the table should be a property of the form

/// <summary>
/// Get or set the data table
/// </summary>
private DataTable myTable = new DataTable();



You could initialise it at any point, I do in the form constructor, just after the form has been initialised. I also set the source of the DataGridView on the main form to the table...

// initialise
this.initialiseTable(this.myTable);

// set source
this.dataGridView1.DataSource = this.myTable;



And then when you add a row, just create it, set all cells, and add it to the table. The DatagridView on the form will be updated automatically...

// create a row
DataRow rd = this.myTable.NewRow();

// set values
rd[0] = textBox1.Text;
rd[1] = textBox2.Text;
rd[2] = textBox3.Text;

// add row to table 
this.myTable.Rows.Add(rd);


This post has been edited by ragingben: 23 February 2011 - 07:15 AM

Was This Post Helpful? 1
  • +
  • -

#7 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5681
  • View blog
  • Posts: 12,223
  • Joined: 02-June 10

Re: add data from textbox into datagridview

Posted 23 February 2011 - 07:20 AM

View Postmonica hinge, on 23 February 2011 - 05:24 AM, said:

i tried a lot to add data from text box into datagridview on button click..
i m new to c# and really want help from you...is these is the right one bcoz..its show error..
Thanks You in Advance..


Really? With all the big bold comments about posting your code in code tags you still couldn't be bothered to do it?

Attached Image


Also, dis not b d'hood dawg... You are not here texting your high school posse to come to your kegger after their shift at Taco Bell. You are here asking for help from senior coding professionals who graciously donate their valuable time to helping the next generation of coders with their chosen craft. Please try to show them, yourself and the industry some respect by writing at least at an eighth grade level. (IE: English not ebonics or SMS, real words, punctuation and so on)

If you can't take your own problem/question seriously enough to write like an adult, then why would you expect anyone else to take it seriously?
Was This Post Helpful? 2
  • +
  • -

#8 monica hinge  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-February 11

Re: add data from textbox into datagridview

Posted 02 March 2011 - 02:32 AM

Thanks for your help!!!! Currently i m working a lot on datagrid view ..and want my datagridview work on enter and not on Tab control..like gridview should work Like Excel..On enter curser should move to next cell..
In gridview i hv combobox ..as i select item from combo box other cell should be fill autometically with some data..but it is possible only by tab event and not enetr event..
i want my gridview work on enter...
the code is something like this..
 private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            
            try
            {
                for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                   

                    if (dataGridView1.Rows[i].Cells[3].Value != null && dataGridView1.Rows[i].Cells[5].Value != null)
                    {
                        double a;
                        a = Convert.ToDouble(dataGridView1.Rows[i].Cells[3].Value) * Convert.ToDouble(dataGridView1.Rows[i].Cells[5].Value);
                        dataGridView1.Rows[i].Cells[6].Value = a.ToString();
                    }

                    if (dataGridView1.Rows[i].Cells[0].Value != null)
                    {
                        string abc = dataGridView1.Rows[i].Cells[0].Value.ToString();
                        string ab = "select ID,ItemName,Brand,Unit from Item where ID ='" + abc + "'";
                        da8 = new OleDbDataAdapter(ab, con);
                        dtb1 = new DataTable();
                        da8.Fill(dtb1);

                        for (int j = 0; j < dtb1.Rows.Count; j++)
                        {
                            //MessageBox.Show("" + dt.Rows.Count);
                            if (dtb1.Rows[j]["ID"].ToString() == abc)
                            {
                                dataGridView1.Rows[i].Cells[1].Value = dtb1.Rows[j]["ItemName"].ToString();
                                dataGridView1.Rows[i].Cells[2].Value = dtb1.Rows[j]["Brand"].ToString();
                                dataGridView1.Rows[i].Cells[4].Value = dtb1.Rows[j]["Unit"].ToString();
                                // dataGridView1.Rows[i].Cells[0].Value = dtb.Rows[j]["ItemName"].ToString();
                            }
                        }
                       
                    }
  //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                    if (dataGridView1.Rows[i].Cells[1].Value != null)
                    {
                        string abc = dataGridView1.Rows[i].Cells[1].Value.ToString();
                        string ab = "select ID,ItemName,Brand,Unit from Item where ItemName ='" + abc + "'";
                        da8 = new OleDbDataAdapter(ab, con);
                        dtb1 = new DataTable();
                        da8.Fill(dtb1);

                        for (int j = 0; j < dtb1.Rows.Count; j++)
                        {
                            //MessageBox.Show("" + dt.Rows.Count);
                            if (dtb1.Rows[j]["ItemName"].ToString() == abc)
                            {

                                dataGridView1.Rows[i].Cells[0].Value = dtb1.Rows[j]["ID"].ToString();
                                dataGridView1.Rows[i].Cells[2].Value = dtb1.Rows[j]["Brand"].ToString();
                                dataGridView1.Rows[i].Cells[4].Value = dtb1.Rows[j]["Unit"].ToString();


                            }

                        }
                    }
                  

                }
                dataGridView1.SelectedCells[3].DataGridView.EditingControl.Focus();
            }
            catch (Exception)
            {

            }
                
        }

there is the screen snap shot of my form.
thanks in advance :rolleyes:

This post has been edited by insertAlias: 02 March 2011 - 09:34 AM

Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5681
  • View blog
  • Posts: 12,223
  • Joined: 02-June 10

Re: add data from textbox into datagridview

Posted 02 March 2011 - 05:33 AM

Monica: Had you considered that when you so flagrantly disregard the guidelines of this site by absolutely refusing to do something as simple as wrapping your code in code tags that you are really just thumbing your nose at the very people you are asking for help from?

If you can't invest an extra 3 seconds to use the code tags then why should people here invest considerably more time in your problem?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1