Can anyone explain why btnHandler would throw an error "Column 'Price' does not belong to table" and how to get the btnHandler class values into the Form?
Thanks,

//This is the main form
namespace TestProject
{
  public partial class Test : Form
    {
      public DataGridView GridView1 = new DataGridView();
      public DataTable dtg = new DataTable();
      public string s0, s1, s2, s3, s4;
      
      public Test()
      {
        InitializeComponent();
      }
        private void Test_Load_1(object sender, EventArgs e)
        {
          Application.Idle += new EventHandler(app_Idle);
        }
         private void btn_Events(object sender, EventArgs e)
        {
            BtnHandler newBtn = new BtnHandler();
            newBtn.btn_Events(sender,e);
        }
         public void ADD()
        {
          Test mf = new Test();
          DataRow dr = this.dtg.NewRow();
          dr["Price"] = s0;
          dr["Name"] = s1;
          this.dtg.Rows.Add(dr);
        }
        public void TestGridUpdate(object sender, EventArgs e, string ItemPrice,string btnName)
        {
          s0 = ItemPrice;
          s1 = sender.ToString();
          // IF CALLED BY btnHandler. THROWS ERROR.
          this.ADD();
        }
        public void TestMakeGrid()
        {
          BindingSource GridBinder = new BindingSource();
          GridBinder.DataSource = dtg;
          GridView1.DataSource = GridBinder;

          this.GridView1.Size = new Size(200, 200);
          this.GridView1.Location = new Point(100, 100);
          DataColumn dc;
          dc = new DataColumn("Price", typeof(string));
          dtg.Columns.Add(dc);
          dc = new DataColumn("Name", typeof(string));
          dtg.Columns.Add(dc);

          this.GridView1.DataSource = dtg;
          Test.ActiveForm.Controls.Add(this.GridView1);
        }
        private void app_Idle(object sender, EventArgs e) 
        { Application.Idle -= new EventHandler(app_Idle); 
          this.TestMakeGrid();
          ////trick the btnhandler into thinking it got sent a button request to load something
          Button fakeSend = new Button();
          fakeSend.Text = "Load Data";
          fakeSend.Name = "btn_Data";
          btn_Events(fakeSend, e);  //btnHandler will load data and try to run ADD
        }
    } 
} 

//This is in a class called btnHandler.cs
namespace TestProject
{
    class BtnHandler
    {
       public void btn_Events(object sender, EventArgs e)
        {
          Button btnsender = (Button)sender;
          BtnHandler newBtn = new BtnHandler();
          string lsender = sender.ToString().Remove(0, sender.ToString().IndexOf(":") + 2);
        
          Test Mainfrm = new Test();
          Mainfrm.TestGridUpdate(lsender, e, "1.90", btnsender.Name);
        }
    } 
}

Recommended Answers

All 4 Replies

1st, about you the error "column Price" doesn not belong to table:
YOu have to create columns before filling the table up. YOu do it like:

DataTable table = new DataTable("TableName");
table.Columns.Add(new DataColumn("Price", typeof(decimal))); //specify the correct value type
table.Columns.Add(new DataColumn("Name", typeof(string)));
//then you can fill it up...
;

2nd question about transfering the dataTable to the class:
You can simply pass it as a parameter to some method:
for example you have a method where you fill the table up, and then pass the table as parameter to another method in the same or other class:

class Class1
{
    private void ShowDataFromDataTable()
    {
        DataTable table = FillingDataTable();
        //when code comes back to here, table is fill up with data, 
        //you can use data from it to populate dgv, or something else
    }
    public DataTable FillingDataTable() //modifier can be private if you call it from the same class!
    {
        //example with only 1 column
        DataTable table = new DataTable();
        table.Columns.Add(new DataColumn("1st column", typeof(string)));
        //fill the table
        return table;
    }
}

class Class2
{
    private void ShowDataFromDataTableInOtherClass()
    {         
        DataTable table = FillingDataTable();
        //when code comes back to here, table is fill up with data, 
        //you can use data from it to populate dgv, or something else
    }
}

I hope this is clear example of how to transfer values (data) form method to method, or from class to class in a run time!

Hope this helps,

Mitja

Thanks a bunch Mitja. I'll study this a bit. My code is a bit more complex than the example I posted. I've created about 30 buttons dynamically in a buttonhandler class and when I try to pass an object (like an array or datatable) to the part similar to your FillingDataTable, the part of my code where it creates the dynamic button events throws an error.
Thanks again. nice to know someone took a look. :- )

No big deal, just let me know, if you are satisfied with what I have gaven you. If not, please let me know. I`ll try to do better, ok?

cya
Mitja

Hi

Thank you ..This code is very helpful to us in our project

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.