问题描述
我有MainForm和Form2显示在Mainform的面板中。(看起来像MDI形式但不是MDI,因为我听说MDIForm是坏主意)
当我检查Mainform.cs中Form2上显示的datagridview1的RowCount或
时,datagridview1.RowCount总是一个。
我还发现 Form2.IsHandleCreated总是假的。甚至如果我看到Mainform面板中显示的Form2。
所以我通过 CreateControl 方法为Form2和datagridview1创建了句柄。
但仍然是Form2.IsHandleCreated为false。
datagridview1.ISHandle是假的ALWAYS.WHY?为什么?为什么?
也试过
我已经尝试了很多但没有找到任何解决方案。
我知道如何使用委托从不同的线程中写入值。
我已经从线程中写了Maindatagridview的值,因为我可以看到它
RowCount是我输入的内容(不止一个)。
所以我只想要解决方案为什么RowCount总是儿童Form2中的一个。
每个孩子的表格都会发生这种情况。
这个问题的问题我无法从任何其他形式向datagridview写入值
表格,甚至来自Mainform。
问题改进
//
代码1
我如何在MainForm中创建Form2
I have MainForm and Form2 is displayed in panel of Mainform.(Looks like MDI form but not MDI as I have heard that MDIForm is bad idea)
When I Check the RowCount Of datagridview1 displayed on Form2 in Mainform.cs or
any other form the datagridview1.RowCount is always one.
I have also found that Form2.IsHandleCreated is always false .Even If I see the Form2 displayed in Mainform panel.
So I have created handle by CreateControl method for Form2 and datagridview1.
but still Form2.IsHandleCreated is false.
datagridview1.ISHandle is false ALWAYS.WHY?WHY?WHY?.
also tried
I have tried a lot but not found any solution for this.
And I know how to use delegate to write values from different thread.
I have written values of Maindatagridview from thread because I can see Its
RowCount is What I have entered(more than one).
So I only want the solution for why RowCount is always one in Child Form2.
This is happening with every child form.
Becasue of this problem I am not able to write values to datagridview from any other
form ,even from Mainform.
Question Improved
//
Code 1
How I have created Form2 In MainForm
private Form CreateForm(ControlsEnum frm)
{
switch (frm)
{
case ControlsEnum.Form2:
return new Form2();
case... number of forms
default: return null;
}
}
public void ShowForm(ControlsEnum frm)
{
Form new_Frm = null;
//If dictionary already contains instance of //Idictionary Interface is used
if (controls.ContainsKey(frm))
{
new_Frm = controls[frm];
}
else
{
new_Frm = CreateForm(frm);
controls[ctrl] = new_Frm;
new_Frm.TopLevel = false;
}
new_Frm.Parent = this;
new_Frm.Dock = DockStyle.Fill;
new_Frm.BringToFront();
ShowFormPanel_PNL.Controls.Add(new_ctrl); //this panel is in Mainform
new_ctrl.Show();
////I have use stack to remove previos form //code deleted
}
void Mainform_Load()
{
// ShowForm(ControlEnum.Form2);
if replaced the above line with below code I get results
f2 = new Form2();
f2.TopLevel = false;
ShowFormPanel_PNL.Controls.Add(f2);
f2.Show();
}
//显示Form2和Mainform时。我从Form2上的按钮动态地向gridview添加行。
//然后我点击MainFormbutton1
// When Form2 and Mainform displayed. I add dynamically rows to gridview from a button on Form2.
//then I click MainFormbutton1
private void MainFormbutton1_Click(object sender, EventArgs e)
{
Program.f2 = f2;
int x= Program.f2.From2GridView.Rows.Count;
int y = f2.From2GridView.Rows.Count;
// I can see the expected result.
// but if replaced with ShowForm Rowcount is always 1.
}
推荐答案
这篇关于当以其他形式调用时,DataCridview的RowCount为Always One的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!