2
Answers

passing textbox and combox value into gridview

Hi friends, I have two textbox and two combobox and two radio button and one girdview and one button. I want to insert data from the textbox and combobox to gridview based on the radiobutton clickevent. if radiobutton one is cliked then datagridview column header changed based on the radiobutton.and also data in gridview add row by row. if any example available please give me thank you
Answers (2)
0
kani kani

kani kani

NA 4 0 12y
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
{
 public Form1()
 {
 InitializeComponent();
}

 DataTable DT1 = new DataTable();
 DataTable DT2 = new DataTable();

 private void Form1_Load(object sender, EventArgs e)
 {
 cboBox1.Items.Add("Apple");
 cboBox1.Items.Add("Grapes");
 cboBox2.Items.Add("Rose");
 cboBox2.Items.Add("Lily");
 CreateDT();
 }

 private void CreateDT()
{
 DT1 = new DataTable();
 DT1.Columns.Add(new DataColumn("SNO",typeof (string )));
 DT1.Columns.Add(new DataColumn("FruitsName", typeof(string)));
 
 DT2 = new DataTable();
 DT2.Columns.Add(new DataColumn("SNO", typeof(string)));
 DT2.Columns.Add(new DataColumn("FlowersName", typeof(string)));
 }

private void btnInsert_Click(object sender, EventArgs e)
 {
 dataGridView1.DataSource = null;
 if (rdoFruits.Checked == true)
 {
 if( txtbox1 .Text !="" && cboBox1 .Text !="")
{
 DT1.Rows.Add(new object[] {txtbox1 .Text ,cboBox1 .Text });
 }
 dataGridView1.DataSource = DT1;
 } else if (rdoflowers .Checked == true )
{
 if (txtBox2 .Text != "" && cboBox2.Text != "")
{
DT2.Rows.Add(new object[] { txtBox2.Text, cboBox2.Text });
 }
dataGridView1.DataSource = DT2;
} dataGridView1.Refresh();
txtbox1.Text = string.Empty;
cboBox1.Text = "";
txtBox2.Text = string.Empty;
 cboBox2.Text = "";
 }
 private void rdoFruits_CheckedChanged(object sender, EventArgs e)
{
 dataGridView1.DataSource = null;
 if (rdoFruits.Checked == true)
   {
 dataGridView1.DataSource = DT1;
 dataGridView1.Refresh();
   }
   else if (rdoflowers .Checked == true)
  {
  dataGridView1.DataSource = DT2; dataGridView1.Refresh();
   }
}
}
}
0
divya

divya

NA 219 92.2k 12y
 hai baskaran,


use below code this may helps you

//declare data table globally
private DataTable myTable = new DataTable();
//write these two statements in the constucor
 this.initialiseTable(this.myTable);
 this.GvSellCd.DataSource = this.myTable;
//write this method to add headres to gridview
private void initialiseTable(DataTable table)
        {
            // add all columns
            table.Columns.Add(new DataColumn("CD ID"));
            table.Columns.Add(new DataColumn("Item Name"));
            table.Columns.Add(new DataColumn("Item Description"));
            table.Columns.Add(new DataColumn("Cost"));
            table.Columns.Add(new DataColumn("Quantity"));
            table.Columns.Add(new DataColumn("Amount"));
        }
//write these code click event of radio button
                            DataRow rd = this.myTable.NewRow();
                            // set values
                            rd[0] = txtItemNo.Text;
                            rd[1] = LblItemName.Text;
                            rd[2] = lblItemDescription.Text;
                            rd[3] = lblCdCost.Text;
                            rd[4] = Quantity.ToString();
                            rd[5] = lblCdCost.Text;
                           this.myTable.Rows.Add(rd);