Alex's Weblog

Weblog di Ermanno Goletto (Codename Alex - A Learning EXperience)
posts - 438, comments - 11030, trackbacks - 468

My Links

News

Il blog è stato
trasferito al
 seguente link:


DevAdmin Blog

Add my blog to Live

Foto

Curriculum Vitae


Il contenuto di questo blog e di ciascun post viene fornito “così come é”, senza garanzie, e non conferisce alcun diritto. Questo blog riporta il mio personale pensiero che non riflette necessariamente il pensiero del mio datore di lavoro.

Logo Creative Commons Deed


Logo MCTS

Logo MCSA

Logo MCP

Logo Microsoft Certified Business Management Solutions Professional

Microsoft Certified Business Management Solutions Specialist


Logo UGIdotNET UGIdotNET Contributor


Logo UGISS UGISS Contributor


Logo SysAdmin.it SysAdmin.it Staff


Article Categories

Archives

Post Categories

Blogs

Database

Development

Friends

IT

Knowledge Base

Links

MBS

MCP

MVP Sites

User Groups

Virtualization

Autosizing delle righe di un DataGrid

A volte mi capita di dover mettere mano a codice .NET 1.1 su Visual Studio 2003 e in questa versione la griglia nativa il DataGrid non era ricco di funzionalità. L'ultima richiesta che mi è arrivata è come realizzare l'autosizing delle righe per vedere completamente il contenuto delle righe le cui colonne vano su più righe. Manualment la cosa è possibiole quindi la funzionalità in effetti c'è ma non è esposta e quindi occorre utilizzare la reflection come indicato in questo link http://www.syncfusion.com/FAQ/winforms/FAQ_c44c.aspx

Di seguito la versione che ho realizzato:

Private Sub AutoSizeRows(ByVal columnStyle As System.Windows.Forms.DataGridColumnStyle)
  Dim dataGrid As System.Windows.Forms.DataGrid = columnStyle.DataGridTableStyle.DataGrid
  Dim dataGridtype As System.Type = dataGrid.GetType

  Do
    dataGridtype = dataGridtype.BaseType
    If dataGridtype Is Nothing Then Exit Sub
  Loop Until dataGridtype.FullName = GetType(System.Windows.Forms.DataGrid).FullName
 
  While dataGridtype.FullName <> GetType(System.Windows.Forms.DataGrid).FullName
    dataGridtype = dataGridtype.BaseType
  End While

  Dim mi As System.Reflection.MethodInfo = dataGridtype.GetMethod("get_DataGridRows", _
    Reflection.BindingFlags.FlattenHierarchy Or _
    Reflection.BindingFlags.IgnoreCase Or _
    Reflection.BindingFlags.Instance Or _
    Reflection.BindingFlags.NonPublic Or _
    Reflection.BindingFlags.Public Or _
    Reflection.BindingFlags.Static)

  Dim dataGridRows As New ArrayList

  For Each item As Object In CType(mi.Invoke(dataGrid, Nothing), Array)
    If item.ToString().EndsWith("DataGridRelationshipRow") Then
      dataGridRows.Add(item)
    End If
  Next

  Dim bmb As System.Windows.Forms.BindingManagerBase = _
    dataGrid.BindingContext(dataGrid.DataSource, dataGrid.DataMember)

  If Not bmb Is Nothing AndAlso bmb.Count > 0 Then
    Dim size As System.Drawing.SizeF
    Dim g As System.Drawing.Graphics = _
      System.Drawing.Graphics.FromHwnd(dataGrid.Handle)
    Dim sf As New StringFormat(StringFormat.GenericTypographic)
    Dim columnIndex As Integer = columnStyle.DataGridTableStyle.GridColumnStyles.IndexOf(columnStyle)
    Dim pi As System.Reflection.PropertyInfo
    Dim rowHeight As Integer

    For rowIndex As Integer = 0 To bmb.Count - 1
      size = g.MeasureString(dataGrid(rowIndex, columnIndex).ToString(), dataGrid.Font, columnStyle.Width, sf)
      pi = dataGridRows(rowIndex).GetType().GetProperty("Height")
      rowHeight = System.Convert.ToInt32(size.Height)

      'Little extra cellpadding space
      rowHeight += 8

      pi.SetValue(dataGridRows(rowIndex), rowHeight, Nothing)
    Next

    'Rilascio risorse
     g.Dispose() : g = Nothing
     sf.Dispose() : sf = Nothing
  End If
End Sub

Print | posted on Friday, May 16, 2008 3:59 PM | Filed Under [ Links Code & Snippet .NET Framework ]

Feedback

Gravatar

# User links about &quot;risorse&quot; on iLinkShare

User links about &quot;risorse&quot; on iLinkShare
5/5/2009 11:03 AM | Pingback/TrackBack
Gravatar

# How while on adderall xr.

Adderall in mexico.
6/26/2009 7:33 PM | Adderall tweaked.
Gravatar

# Methotrexate.

Methotrexate for rheumatoid arthritis. Hcg levels up after shot of methotrexate. Methotrexate. Methotrexate treatment for psoriatic arthritis.
Gravatar

# Cozaar.

Cozaar.
6/30/2009 6:01 AM | Cozaar side effects.
Gravatar

# Clonazepam.

Clonazepam and weight gain. What is clonazepam. Clonazepam. Clonazepam side effects. Clonazepam withdrawal symptom.
6/30/2009 3:36 PM | Clonazepam.
Gravatar

# Adderall.

Buy adderall. Adderall. Adderall idiopathic edema. Smoking adderall. Adderall dosage.
7/2/2009 7:11 AM | Adderall xr side effects.
Gravatar

# Bactrim.

Bactrim-ds. What does the pill bactrim look like. Bactrim ds. Bactrim. Heart irregularities with bactrim. Bactrim medication used for.
7/10/2009 6:12 PM | Should i take bactrim.
Gravatar

# Risperdal.

Side effects of risperdal. Risperdal side effects.
7/11/2009 5:11 AM | Risperdal.
Gravatar

# Augmentin side effects.

Augmentin r for pneumonia. Freezing augmentin. Zithromax augmentin. Augmentin.
8/19/2009 4:11 AM | Augmentin.
Gravatar

# Is lotensin a beta blocker.

Lotensin and pregnancy.
8/21/2009 5:06 PM | Lotensin.
Gravatar

# Can diovan cause asthmal.

Diovan blood pressure medication. Diovan hct generic. Generic diovan. Diovan side effects. Diovan. Diovan hct.
8/22/2009 4:08 AM | Diovan generic.
Gravatar

# Norvasc overnight.

Norvasc. Generic norvasc.
8/22/2009 3:35 PM | Norvasc versus lopressor.
Gravatar

# Xenical vs. liptopril.

Xenical scam. Xenical hgh phentermine quit smoking detox. Xenical. Xenical canadian. Xenical success stories. Xenical 120.
8/22/2009 5:08 PM | Xenical without prescription.
Gravatar

# simple logo

Autosizing delle righe di un DataGrid
9/27/2014 4:49 PM | simple logo
Gravatar

# re: Autosizing delle righe di un DataGrid

Thanks alot. i like this site

ديزل ژنراتور<br>
کمپرسور<br>
گيربکس<br>
موتور برق<br>
الکتروموتور<br>
الکتروپمپ<br>
تابلو برق<br>
کارواش<br>
3/17/2016 8:43 AM | LHHLLH
Gravatar

# Check Cashing Costa-mesa

mainly well written file! My organization is rather astounded to read the paper this content. Everybody challenge is extremely applied in conjunction with worthy. Indulge in your effort to put in writing this content. At this time go here Check Cashing Costa-mesa Thank you so much considerably.
10/21/2017 8:25 AM | Pervez
Gravatar

# re: Autosizing delle righe di un DataGrid

Magento Website Development
Magento Support and Maintenance
5/30/2018 12:56 PM | Sam
Gravatar

# re: Autosizing delle righe di un DataGrid

Call Center Services in India
BPO Services India
Outsource Telemarketing Services
Website Designing Company in India
6/9/2018 12:13 PM | Shikha
Gravatar

# re: Autosizing delle righe di un DataGrid

I am grateful to have opened this discussion. This question is quite interesting to me. Finally the answer was found
run 3
7/18/2018 11:54 AM | annashetty

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 5 and 6 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET