English 中文(简体)
。 NET VS2010 2. 数据交换 自动安装的ColumnsMode禁止用户重新使用
原标题:.NET VS2010 DataGridView AutoSizeColumnsMode setting prohibits user resizing

I m在2010年视觉演播室使用C#的Im节目中出现过一个 we脏问题。

I m 载有信息的数据GridView,我愿意按照标题重新调整数据网栏,但如果数据比头值长,则允许用户人工转解宇宙。

因此,我改变了我的数据GridView.AutoSizeColumnsMode = Column Header。 数据单 现在,根据头脑,一栏自动转轨,但我突然发现,尽管我的数据GridView.AllowUser ToResizeColumns财产是真实的,但我可以人工将一栏翻。

我的问题是,我如何能够在允许用户重新配置栏目的同时自动地大小我的数据网格观察栏?

最佳回答

解决办法:here 覆盖到C#并经过测试:

for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
   // Size the column header based on the ColumnHeader mode
   dataGridView1.Columns[i].AutoSizeMode = 
      DataGridViewAutoSizeColumnMode.ColumnHeader;
   // Store autosized width
   int colw = dataGridView1.Columns[i].HeaderCell.PreferredSize.Width;
   // Change back to Resize mode
   dataGridView1.Columns[i].AutoSizeMode = 
      DataGridViewAutoSizeColumnMode.None;
   // Set width to calculated above
   dataGridView1.Columns[i].Width = colw;
}

仅从MSDN找到了更好的解决办法。 在显示数据GridView之前,使用AutoResizeColumn功能。 例如:

void Form1_Load(object sender, EventArgs e)
{
   for (int i = 0; i < dataGridView1.Columns.Count; i++)
   {
      dataGridView1.AutoResizeColumn(
         i, DataGridViewAutoSizeColumnMode.ColumnHeader);
   }
}
问题回答

我在谈到同一问题(更多或更少)时敲响了头脑,我可以说出。

我确保确定以下财产:

AllowUserToResizeColumns = True
AutoSizeColumnsMode = None

接着,在我用手法填满我的电网后,我确定如下:

dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)

禁止化学武器组织显然将使用:

dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader)

这本来应该做到,因此,你一度将哥伦再次上了车,否则,自治公司将无所作为,因此用户仍然可以控制。

...My problem was that I forgot that the Columns themselves have properties that need to be taken into account as well. I had set most of the columns to have:

AutoSizeMode = AllCells

这是我的问题的根源。

AutoSizeMode = NotSet

所有这一切都是应该做到的。

这样做的方式更为简单:

First fill your grid with the desired data. Then, set the following properties (you can also set these in the designer of course)

datagridResult.DataSource = YourDataSourceHere
datagridResult.AllowUserToOrderColumns = true;
datagridResult.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;

现在, 添加以下代码:

 private void MainForm_Shown(object sender, EventArgs e)
    {
        Application.DoEvents();
        datagridResult.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
    }

所有这一切:

detailsDataGridView.AutoSizeColumnsMode = 
        DataGridViewAutoSizeColumnsMode.AllCells doesn t work? 

创建网站here

如果是这样的话,每当一个单位的数据变化等于最大的电池时,你就能够以人工方式处理,并立即转而处理。

守则应当如此,希望这将有助于这样做。

datagridView1.Columns[columnindex].AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...