1

This is a demo project.

What i am trying to achieve is, when a button is clicked it will check certain condition then will insert rows into Datagridview.

The thing is i need to insert rows again with that of existing rows present in the datagridview, with the radio button conditions applied (both new row and updating).

As so far i have achieved this much.

private void button5_Click(object sender, EventArgs e)
    {
        if (button5.Text == "")
        {
            MessageBox.Show("No Value Assigned");
        }
        else
        {
        MySqlConnection con = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["db"].ConnectionString);
        con.Open();
        MySqlDataAdapter dt = new MySqlDataAdapter();
        DataTable tt = new DataTable();
        BindingSource bs = new BindingSource();
        if (dataGridView1.RowCount > 0)
        {                
            bs = (BindingSource)dataGridView1.DataSource;
        }
        else
        { 
                if (radioButton1.Checked == true)
                {

                    string query = "SELECT type,priceS FROM service WHERE type='" + button5.Text + "'";
                    MySqlCommand cmd = new MySqlCommand(query, con);
                    dt.SelectCommand = cmd;                       
                }
                else if (radioButton2.Checked == true)
                {
                    string query = "SELECT type,priceM FROM service WHERE type='" + button5.Text + "'";
                    MySqlCommand cmd = new MySqlCommand(query, con);
                    dt.SelectCommand = cmd;

                }
                else if (radioButton3.Checked == true)
                {
                    string query = "SELECT type,priceL FROM service WHERE type='" + button5.Text + "'";
                    MySqlCommand cmd = new MySqlCommand(query, con);
                    dt.SelectCommand = cmd;                        
                }
            }            
        dt.Fill(tt);
        bs.DataSource = tt;
        dataGridView1.DataSource = bs;
        dt.Update(tt);
        con.Close();
        }            
    }

I can Insert first row, but when i try to insert another with the same button problem.

Any idea is appreciated.

private void button5_Click(object sender, EventArgs e)
    {
        if (button5.Text == "")
        {
            MessageBox.Show("No Value Assigned");
        }
        else
        {
        MySqlConnection con = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["db"].ConnectionString);
        con.Open();
        MySqlDataAdapter dt = new MySqlDataAdapter();
        DataTable tt = new DataTable();
        BindingSource bs = new BindingSource();
        if (radioButton1.Checked == true)
        {

            string query = "SELECT type,priceS FROM service WHERE type='" + button5.Text + "'";
            MySqlCommand cmd = new MySqlCommand(query, con);
            dt.SelectCommand = cmd;
        }
        else if (radioButton2.Checked == true)
        {
            string query = "SELECT type,priceM FROM service WHERE type='" + button5.Text + "'";
            MySqlCommand cmd = new MySqlCommand(query, con);
            dt.SelectCommand = cmd;

        }
        else if (radioButton3.Checked == true)
        {
            string query = "SELECT type,priceL FROM service WHERE type='" + button5.Text + "'";
            MySqlCommand cmd = new MySqlCommand(query, con);
            dt.SelectCommand = cmd;
        }
        if (dataGridView1.RowCount > 0)
        {
            tt.Rows.Add(dataGridView1);
            dt.Fill(tt);
            bs.DataSource = tt;
            dataGridView1.DataSource = bs;
            dt.Update(tt);
            con.Close();
        }
        else
        {
            dt.Fill(tt);
            bs.DataSource = tt;
            dataGridView1.DataSource = bs;
            dt.Update(tt);
            con.Close();
        }
        }            
    }

this is my another method but i am getting Error: Input array is longer than the number of columns in this table. Where am I making the mistake?

  • Have you tried debugging to see what happens on the three different Radio checks. That is where I would start. – Mr. B Oct 29 '15 at 19:35

2 Answers2

0

Hmm, each button click clears the datatable... Maybe define datatable with other function and manually add rows to datatable?

c4pricorn
  • 3,471
  • 1
  • 11
  • 12
  • Please be more elaborate and tell me how to add the new row with some codings... i am not an expert – Account Unknown Oct 30 '15 at 08:10
  • Good explanation is here: https://msdn.microsoft.com/pl-pl/library/z16c79x4%28v=vs.110%29.aspx – c4pricorn Oct 30 '15 at 10:14
  • You can also read http://stackoverflow.com/questions/10063770/how-to-add-a-new-row-to-datagridview-programmatically – c4pricorn Oct 30 '15 at 10:23
  • I think you don't understand my problem, see when the button is clicked it checks whether there is row or not, if there is now it Insets based on radiobutton conditions, now if there is (this where the problem starts) i need to insert the existing with that of newly keyed in value... if (dataGridView1.RowCount > 0) please check this condition. and if you can tell me where is my problem is... – Account Unknown Oct 30 '15 at 12:16
0

I will show you an example with code some code and explanations in the comments, take a look:

First, build the following Form:

Form

And here is its code behind:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Initially your DataGridView will not have a DataSource to display your data so you need
            // to define a DataTable that will be used as the DataSource for you DataGridView
            var dt = new DataTable();
            // Add the desired columns to it, with their headers and data types
            dt.Columns.Add(new DataColumn("Id", typeof(int)));
            dt.Columns.Add(new DataColumn("Description", typeof(string)));

            // You can add new rows to your DataTable using the DataTable object itself
            // in order to create DataRows with the exact DataColumns required for that DataTable
            var newRow = dt.NewRow();
            newRow["Id"] = 1;
            newRow["Description"] = "Desc 1";
            // And then you simply add this new row to your DataTable
            dt.Rows.Add(newRow);

            newRow = dt.NewRow();
            newRow["Id"] = 2;
            newRow["Description"] = "Desc 2";
            dt.Rows.Add(newRow);

            newRow = dt.NewRow();
            newRow["Id"] = 3;
            newRow["Description"] = "Desc 3";
            dt.Rows.Add(newRow);

            // By the end you set the DataSource property of your DataGridView
            // assigning to it the DataTable you created
            dataGridView1.DataSource = dt;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // In order to add new rows to your DataGridView with the current DataTable
            // you simply need to take it back from the DataGridView.DataSource property by casting it
            // to a DataTable again
            var dt = (DataTable)dataGridView1.DataSource;

            // And then you add the new rows in the same way that were shown in the Click event for button1
            var newRow = dt.NewRow();
            newRow["Id"] = 4;
            newRow["Description"] = "Desc 4";
            dt.Rows.Add(newRow);

            newRow = dt.NewRow();
            newRow["Id"] = 5;
            newRow["Description"] = "Desc 5";
            dt.Rows.Add(newRow);
        }
    }
}
Zignd
  • 6,896
  • 12
  • 40
  • 62