Breaking News

LightBlog

Wednesday, 6 January 2016

Add data from Textbox to GridView on button click

C# 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;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Drawing.Imaging;
namespace Punit
{
    public partial class Generate_Bill : Form
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["admin"].ConnectionString);
        private DataTable myTable = new DataTable();
        public Generate_Bill()
        {
            InitializeComponent();
            this.initialiseTable(this.myTable);
            productbind();
            this.dataGridView1.DataSource = this.myTable;
            this.dataGridView2.DataSource = this.myTable;
        }

        private void productbind()
        {
            SqlDataAdapter da = new SqlDataAdapter("select * from Admin_Category", con);
            DataSet ds = new DataSet();
            da.Fill(ds, "Admin_Category");
            if (ds.Tables.Count > 0 && ds.Tables["Admin_Category"].Rows.Count > 0)
            {

                comboBox1.DataSource = ds.Tables[0];
                comboBox1.ValueMember = "Category_Id";
                comboBox1.DisplayMember = "Category";
            }
       
            con.Close();
        }

        private void producttype()
        {
            SqlDataAdapter da = new SqlDataAdapter("select * from Admin_Product where Category='" + comboBox1.Text + "'", con);
            DataSet ds = new DataSet();
            da.Fill(ds, "Admin_Product");
            if (ds.Tables.Count > 0 && ds.Tables["Admin_Product"].Rows.Count > 0)
            {

                comboBox2.DataSource = ds.Tables[0];
                comboBox2.ValueMember = "Product_Id";
                comboBox2.DisplayMember = "Product_Name";
            }
         
            con.Close();
        }
        private void initialiseTable(DataTable table)
        {
            table.Columns.Add(new DataColumn("Sr.No"));

            table.Columns.Add(new DataColumn("Product Name"));

            table.Columns.Add(new DataColumn("Product Type"));

            table.Columns.Add(new DataColumn("Weight"));

            table.Columns.Add(new DataColumn("Rate"));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label21.Text = textBox1.Text;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DataRow rd = this.myTable.NewRow();
            rd[0] = comboBox1.Text;

            rd[1] = comboBox2.Text;

            rd[2] = textBox6.Text;
            rd[3] = textBox5.Text;
            this.myTable.Rows.Add(rd);
            prices();
        }