C# DataGridView中的数据在哪里更新?
我通过在选项卡控件的根页面中按下按钮,以编程方式创建一个选项卡页面。目前,所有的页面初始化都是通过点击按钮的方式进行的。在完成了所有的实例化、从文件捕获数据等之后,我最后想调整数据网格视图中的列宽,以便所有行数据都显示出来,而不必有水平滚动条。在你所有贡献者的帮助下,我成功地完成了所有工作,但最后一步除外。以全速运行时,似乎在从表中加载数据完成之前访问了C# DataGridView中的数据在哪里更新?,c#,datagridview,width,wait,C#,Datagridview,Width,Wait,我通过在选项卡控件的根页面中按下按钮,以编程方式创建一个选项卡页面。目前,所有的页面初始化都是通过点击按钮的方式进行的。在完成了所有的实例化、从文件捕获数据等之后,我最后想调整数据网格视图中的列宽,以便所有行数据都显示出来,而不必有水平滚动条。在你所有贡献者的帮助下,我成功地完成了所有工作,但最后一步除外。以全速运行时,似乎在从表中加载数据完成之前访问了DataGridView,因为从RegistersGrid.ColumnCount(局部变量l)派生的计数为零,所以该视图失败。如果我一步一步地
DataGridView
,因为从RegistersGrid.ColumnCount
(局部变量l)派生的计数为零,所以该视图失败。如果我一步一步地完成代码,一切都会很好。我假设我需要放置一个某种形式的互斥来等待传输完成,但我无法计算在哪里进行,以便重置标志!如果有人能为我指出正确的方向,或者有更好的更有条理的方法来解决这个问题,我将非常感谢您的帮助:-)
为了以防万一,我已经在方法中包含了所有的代码,我恐怕要追溯到很久以前,所以如果这看起来像是汇编程序、pascal和c的笨拙孩子,加上一点c,我很抱歉……这是我的年龄:-)
private void AddModuleButton_单击(对象发送方,事件参数e)
{
字符串ModuleID=null;
字符串ModuleTypeFileNumber=null;
字符串ModuleType=null;
int-TabID=0;
openFileDialog1.Filter=“文本文件(.txt)|*.txt”;
DialogResult=openFileDialog1.ShowDialog();
if(result==DialogResult.OK)
使用(StreamReader=newstreamreader(openFileDialog1.FileName))
{
字符串换行符;
if((newline=reader.ReadLine())!=null)
{
string[]value=newline.Split((char)9);
ModuleTypeFileNumber=值[1];
}
if((newline=reader.ReadLine())!=null)
{
string[]value=newline.Split();
ModuleID=值[0];
ModuleType=值[1];
}
布尔吸收线=真;
而(吸收线和((换行符=reader.ReadLine())!=null))
{
string[]value=newline.Split();
如果(值[0]=“读数”)吸收线=假;
}
string[]columnnames={“摘要”、“访问”、“寄存器”、“模块和名称”、“值”、“单元”、“类型”、“资源”};
string[]columntypes={“System.Boolean”、“System.string”、“System.Int32”、“System.string”、“System.Int32”、“System.string”、“System.string”、“System.string”};
int[]columnwidth={1,2,3,35,10,5,5};
DataTable dt=新的DataTable();
for(int i=0;iprivate void AddModuleButton_Click(object sender, EventArgs e)
{
string ModuleID = null;
string ModuleTypeFileNumber = null;
string ModuleType = null;
int TabID = 0;
openFileDialog1.Filter = "Text Files (.txt)|*.txt";
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
using (StreamReader reader = new StreamReader(openFileDialog1.FileName))
{
string newline;
if ((newline = reader.ReadLine()) != null)
{
string[] values = newline.Split((char)9);
ModuleTypeFileNumber = values[1];
}
if ((newline = reader.ReadLine()) != null)
{
string[] values = newline.Split();
ModuleID = values[0];
ModuleType = values[1];
}
bool AbsorbLines = true;
while (AbsorbLines && ((newline = reader.ReadLine()) != null))
{
string[] values = newline.Split();
if (values[0] == "Readings") AbsorbLines = false;
}
string[] columnnames = { "Summary", "access", "register", "Module & Name", "Value", "unit", "type", "Res" };
string[] columntypes = { "System.Boolean", "System.String", "System.Int32", "System.String", "System.Int32", "System.String", "System.String", "System.String" };
int[] columnwidth = { 1,2,3,35,10,5,5,5 };
DataTable dt = new DataTable();
for(int i =0; i < columnnames.Length; i++)
{
DataColumn colString = new DataColumn(columnnames[i]);
colString.DataType = System.Type.GetType(columntypes[i]);
dt.Columns.Add(colString);
}
while (ImportTable("Controls", reader.ReadLine(), dt, "RO")) { } // Import the "readings" section
while (ImportTable("Status bits", reader.ReadLine(), dt, "RW")) { } // Import the "controls" section
reader.Close();
registerTables.Add(ModuleID, dt);
// create a new tab page
TabPage page = new TabPage(ModuleID);
InterbusRegisters.TabPages.Add(page);
//
// tabPage1
//
Button ResizeButton = new Button();
Button RemoveButton = new Button();
Label VersionLabel = new Label();
Label SerialNolabel = new Label();
TextBox VersionNoTB = new TextBox();
TextBox SerialNoTB = new TextBox();
DataGridView RegistersGrid = new DataGridView();
//
// Set the properties of the DataGrid.
//
RegistersGrid.AccessibleName = ModuleID + "Grid";
RegistersGrid.Location = new System.Drawing.Point(3,29);
RegistersGrid.Width = page.Width - 6;
RegistersGrid.Height = page.Height - 29;
RegistersGrid.Anchor = (AnchorStyles.Top | AnchorStyles.Left);
RegistersGrid.DataSource = dt;
RegistersGrid.Dock = (DockStyle)2;
//
// RemoveButtonRegistersGrid
//
RemoveButton.BackColor = System.Drawing.Color.Red;
RemoveButton.Location = new System.Drawing.Point(3, 4);
RemoveButton.Name = "RemoveButton";
RemoveButton.Size = new System.Drawing.Size(75, 25);
RemoveButton.TabIndex = 0;
RemoveButton.Text = "Remove";
RemoveButton.UseVisualStyleBackColor = false;
RemoveButton.Click += new System.EventHandler(this.RemoveButton_Click);
//
// ResizeButton
//
ResizeButton.BackColor = System.Drawing.Color.DarkOrange;
ResizeButton.Location = new System.Drawing.Point(81, 4);
ResizeButton.Name = "ResizeButton";
ResizeButton.Size = new System.Drawing.Size(75, 25);
ResizeButton.TabIndex = 6;
ResizeButton.Text = "Resize";
ResizeButton.UseVisualStyleBackColor = false;
ResizeButton.Click += new System.EventHandler(this.ResizeButton_Click);
//
// SerialNolabel
//
SerialNolabel.AutoSize = true;
SerialNolabel.Location = new System.Drawing.Point(159, 10);
SerialNolabel.Name = "SerialNolabel";
SerialNolabel.Size = new System.Drawing.Size(53, 13);
SerialNolabel.TabIndex = 4;
SerialNolabel.Text = "Serial No:";
//
// SerialNoTB
//
SerialNoTB.Location = new System.Drawing.Point(215, 7);
SerialNoTB.Name = "SerialNoTB";
SerialNoTB.Size = new System.Drawing.Size(100, 20);
SerialNoTB.TabIndex = 1;
//
// VersionLabel
//
VersionLabel.AutoSize = true;
VersionLabel.Location = new System.Drawing.Point(318, 10);
VersionLabel.Name = "VersionLabel";
VersionLabel.Size = new System.Drawing.Size(45, 13);
VersionLabel.TabIndex = 5;
VersionLabel.Text = "Version:";
//
// VersionTB
//
VersionNoTB.Location = new System.Drawing.Point(366, 7);
VersionNoTB.Name = "VersionTB";
VersionNoTB.Size = new System.Drawing.Size(100, 20);
VersionNoTB.TabIndex = 2;
page.Controls.Add(ResizeButton);
page.Controls.Add(RemoveButton);
page.Controls.Add(VersionLabel);
page.Controls.Add(VersionNoTB);
page.Controls.Add(SerialNolabel);
page.Controls.Add(SerialNoTB);
page.Controls.Add(RegistersGrid);
page.Location = new System.Drawing.Point(4, 22);
page.Size = new System.Drawing.Size(716, 554);
page.TabIndex = 1;
page.UseVisualStyleBackColor = true;
page.Update(); // the following code fails
int k = dt.Columns.Count;
int l = RegistersGrid.ColumnCount;
for (int j = 0; j <= RegistersGrid.ColumnCount - 1; j++)
RegistersGrid.Columns[j].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
RegistersGrid.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
//datagrid has calculated it's widths so we can store them
for (int i = 0; i <= RegistersGrid.ColumnCount - 1; i++)
{
int colw = RegistersGrid.Columns[i].Width; //store autosized widths
RegistersGrid.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None; //remove autosizing
RegistersGrid.Columns[i].Width = colw; //set width to calculated by autosize
}
}
}