how can we load valuse to a data grid from textbox...without saving in database..and i also need to save the data from the grid to db when clicking the submit button

Recommended Answers

All 6 Replies

You can prepare List<T> or use DataTable.

You don't have to use textboxes to fill a datagrid.
The default colomns in a datagrid are textboxes.

The default colomns in a datagrid are textboxes.

Annoyingly substandard textboxes if you need more than default behavior, but yes. ;) It's also an interesting exercise in frustration if you want to write an extension of the standard textbox column. I'm not surprised there are so many replacements for the datagrid and datagridview controls.

IMOHO you could spend a lifetime learning ALL the possibilities possible with DataGrid and DataGridView... But he! They come in handy by times, if you know how to google for a solution and can find one.

Textbox is by default in Datagrid.
use Rowhandle to use the row and its values in columns and this code should fall under the Submit button implementation.
This should solve your problem.

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;
using System.Data.SqlClient;

namespace HPSL_SAP
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        int i = 0;
        private void bADD_ValueDGV_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Add();// ADD new Row in DataGridvIew
            dataGridView1.Rows[i].Cells[0].Value = textBox1.Text;//Sending Value from textbox to Datagridview
            i++;
        }
         SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        public void connection()
        {
            try
            {
                con.Open();
            }
            catch
            {
                con.Close();
                con.Open();
            }
        }
        private void bSAVE_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                string name = Convert.ToString((dataGridView1.Rows[i].Cells[0] as DataGridViewTextBoxCell).FormattedValue.ToString());// converting the datagridview textbox into string
                connection();
                string aql="insert into table1(name)values('"+name+"')";
                SqlCommand cmd = new SqlCommand(sql, con);
                int insert = cmd.ExecuteNonQuery();
                if (insert > 0)
                {
                    MessageBox.Show("Successfully Saved", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }

        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.