I want to call the Main function of class Program, and pass a sql string to the Main fuction,
from the function of btn1Search_Click of the class Form1, when i click a button on the main form.
i am writing a project for database, and all the data put into the dataGridView. and i want the window displays another data when i click a button.
Could anyone tell me how to do that, please.
///////////////////////////////
static class Program
{
public static string sql;
///
/// The main entry point for the application.
///
[STAThread]
static void Main(string[] args)
{
if (args.Length < 1)
sql = "select buyerName, id, category, productName, price, date, referenceNo from elena ";
else
sql = args[0];
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1(sql));
}
}
/////////////////////////////
public partial class Form1 : Form
{
........
private void btn1Search_Click(object sender, EventArgs e)
{
}
...........
}
Walter KiessPosted Dec 12, 2012, 11:18 PM
Walter KiessPosted Dec 13, 2012, 12:28 AM
kamPosted Dec 13, 2012, 12:24 AM
i create a class called loader
public Loader(string sql)
{
Form1 f1 = new Form1(sql);
f1.Show();
}
and in the Form1,
private void btn1Search_Click(object sender, EventArgs e)
{
this.Hide();
str = "select buyerName, id, category, productName, price, date, referenceNo from elena where referenceNo='d11'";
Loader L = new Loader(str);
}
Thanks a lot!!!!!!!!!!!
Walter KiessPosted Dec 13, 2012, 12:23 AM
private void btn1Search_Click(object sender, EventArgs e)
{
this.Hide();
str = "select buyerName, id, category, productName, price, date, referenceNo from elena where referenceNo='d11'";
Form1_Load(sender, e);
}
You may need to clear the grid first though. not sure...
kamPosted Dec 13, 2012, 12:01 AM
the button, the window still show the same data?????
//////////////////////
public partial class Form1 : Form
{
public static String str;
public Form1(String myStr)
{
InitializeComponent();
str = myStr;
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = Infor.GetDataSet(str);
dataGridView1.DataMember = "elena";
}
private void save_Click(object sender, EventArgs e)
{
Infor.UpdateDataSet((DataSet)dataGridView1.DataSource);
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex >= 0)
{
if (dataGridView1.Columns[e.ColumnIndex].Name == "Column8")
{
dataGridView1.Rows.RemoveAt(e.RowIndex);
}
}
}
private void btn1Search_Click(object sender, EventArgs e)
{
this.Hide();
str = "select buyerName, id, category, productName, price, date, referenceNo from elena where referenceNo='d11'";
Form1 f1 = new Form1(str);
this.Show();
}
}
kamPosted Dec 12, 2012, 11:46 PM
kamPosted Dec 12, 2012, 11:06 PM
private void reload(string sql)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1(sql));
}
and in the Form1
private void btn1Search_Click(object sender, EventArgs e)
{
Program.reload("select buyerName, id, category, productName, price, date, referenceNo from elena ");
}
But the compiler tell me
Error 1 'elena.Program.reload(string)' is inaccessible due to its protection level E:\C#\elena\elena\Form1.cs 62 21 elena
Walter KiessPosted Dec 12, 2012, 10:53 PM
Try this (aircode):
private void btn1Search_Click(object sender, EventArgs e)
{
string sql = "...";
Main(sql);
}
You may need to redefine Main as:
private void Main(string sql)
{...}