namespace TEST { public partial class Form1 : Form { public Form1() { InitializeComponent(); } DataTable dt1 = new DataTable(); DataTable dt2 = new DataTable(); private void button1_Click(object sender, EventArgs e) { Form2 f = new Form2(dt1); f.ShowDialog(); } private void button2_Click(object sender, EventArgs e) { Form2 f = new Form2(dt2); f.ShowDialog(); } } public partial class Form2 : Form { public Form2(DataTable dt) { InitializeComponent(); dataGridView1.DataSource = dt; } } }
试下, 我也做出来了 不过比你的麻烦的多 LS能解释下 Form2 f = new Form2(dt2); f.ShowDialog(); 是什么意思吗? 作用是什么?
這是Form2 public partial class Form2 : Form { public Form2() { InitializeComponent(); } public DataTable Source { set { dataGridView1.DataSource = value; } } }
namespace TEST
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2(dt1);
f.ShowDialog();
} private void button2_Click(object sender, EventArgs e)
{
Form2 f = new Form2(dt2);
f.ShowDialog();
}
}
public partial class Form2 : Form
{
public Form2(DataTable dt)
{
InitializeComponent();
dataGridView1.DataSource = dt;
}
}
}
LS能解释下 Form2 f = new Form2(dt2);
f.ShowDialog();
是什么意思吗?
作用是什么?
public System.Windows.Forms.DataGridView dataGridView2;//form1中:
private Form2 form2;
private void button1_Click(object sender, EventArgs e)
{
if (form2 == null)
{
form2 = new Form2();
form2.Show();
}
form2.dataGridView1.Visible = true;
form2.dataGridView2.Visible = false;
} private void button2_Click(object sender, EventArgs e)
{
if (form2 == null)
{
form2 = new Form2();
form2.Show();
}
form2.dataGridView1.Visible = false ;
form2.dataGridView2.Visible = true ; }
{
string connectString = "Server=(local);Integrated Security=True;Database=northwind";
SqlConnection con = new SqlConnection(connectString);
con.Open();
SqlCommand command = con.CreateCommand();
command.CommandText = "select * from Customers"; DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(command);
adapter.Fill(ds, "Customers"); con.Close(); DataTable t = ds.Tables["Customers"]; Form2 f = new Form2();
f.Source = t;
f.ShowDialog();
}form2中:private DataTable source;
public DataTable Source
{
get
{
return source;
}
set
{
source = value;
}
} private void Form2_Load(object sender, EventArgs e)
{
this.dataGridView1.DataSource = source;
}
public DataTable Source
{
set{ dataGridView.dataSource=value;}
}
Form1 定義一個form2全局變量form2 frm=new form2();
button1_click事件frm.Source=表1button2_click事件
frm.source=表2;
Form2 frm = null;
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Name");
DataRow dr = dt.NewRow();
dr[0] = "張三";
dt.Rows.Add(dr);
frm.Source = dt;
} private void button2_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID");
DataRow dr = dt.NewRow();
dr[0] = "0314";
dt.Rows.Add(dr);
frm.Source = dt;
} private void button3_Click(object sender, EventArgs e)
{
frm = new Form2();
frm.Show();
}
{
public Form2()
{
InitializeComponent();
} public DataTable Source
{
set
{
dataGridView1.DataSource = value;
}
}
}