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);
}
}
}