الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

كيف يمكن التحكم في سعة الاعمدة في data grid

مغلق
بدأه salehgmash في 27 يونيو 2005 · 2 رد · 661 مشاهدة · في Microsoft Visual Basic.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

الاخوان الاعزاء

هل ممكن التحكم في سعة الاعمدة في data grid بحيث يكون مثلا العمود الاول رقم الطالب محدد بعمود يكفي لكتابة رقم الطالب والعمود الاخر مثلا اسم الطالب يكون اتساعه اوسع من رقم الطالب لان الاسم ياخذ سعة اطول من الرقم وكذلك لوسمحتم هل يمكن كتابة عناويين الاعمدة على سطرين مثل ان يكون كلمة رقم على السطر الاول وكلمة الطالب على السطر الثاني حيث انه من الضروري ذلك لان سعة عمود رقم الطالب ستكون صغيرة والله يحفظكم ويعطيكم الصحة والعافية

#2

'Module Created by : Dino Scarcella

'Date : 23-04-2004

'Reason : Needed a module to perform certain functions on datagrids

' parameter 1 : the datagrid you want to autosize

' parameter 2 : the name of the table in your dataset, if this is not right an error will occur TIP: use the name of the dataset table eg. "oDataset.Tables(0).TableName"

' parameter 3 : the padding used when you measure a string in a cell (I use 1 as a default)

' I am just submitting a minor rework of the above authors code.

'Fix: Resize now does throw exceptions on hitting columns that are in the datasoource, but not in TableStyle

'Fix: Resize ignores columns set to 0 width - I am assuming those are intentionally hidden

'Note: Code is streamlined by directly using each GridColumnStyle to loop with

Public Sub AutoSizeDataGridColumns(ByVal dgData As DataGrid, ByVal strMappingName As String, ByVal intPadding As Integer)

Dim ex1 As New Exception("DataGrid must be databound")

Dim ex2 As New Exception("DataSource must be DataTable")

Dim ex3 As New Exception("Invalid Column Name")

Dim dt As DataTable

Dim dr As DataRow

Dim dcCol As DataColumn

Dim gr As Graphics

Dim sngGridNameWidth As Single, sngDataWidth As Single

Dim i As Integer, sngPadding As Single

If dgData.DataSource Is Nothing Then

Throw ex1

Exit Sub

End If

Dim ts As New DataGridTableStyle ' Create a new table style

ts.MappingName = strMappingName ' Address the TableGrid Style

If dgData.TableStyles.Contains(ts.MappingName) = False Then dgData.TableStyles.Add(ts) ' Attach the new style if the table does not have this style

If TypeOf dgData.DataSource Is DataSet Then

dt = CType(dgData.DataSource, DataSet).Tables(dgData.DataMember)

ElseIf TypeOf dgData.DataSource Is DataTable Then

dt = dgData.DataSource

Else

Throw ex2

Exit Sub

End If

If dt.Rows.Count > 0 Then ' Don't change size if there are no rows.

Try

dgData.BeginInit()

dr = dt.Rows(0)

Dim sz As SizeF 'Stores an ordered pair of floating-point numbers, typically the width and height of a rectangle.

gr = dgData.CreateGraphics

sz = gr.MeasureString(New String("M", intPadding), dgData.Font) ' Pad "n" M-width characters

sngPadding = sz.Width

ts = dgData.TableStyles(ts.MappingName)

Dim ccs As DataGridColumnStyle

'Do the headers first

For Each ccs In ts.GridColumnStyles

If Not ccs.Width = 0 Then 'Don't size ones that are intentionally hidden

' Measure the "width" of the text in each grid column header

Try

sz = gr.MeasureString(ccs.HeaderText, dgData.Font)

ccs.Width = sz.Width + sngPadding

Catch ex As ArgumentOutOfRangeException

Throw ex3

End Try

End If

Next

For i = 0 To dgData.VisibleRowCount - 2 ' Calculate width on all visible rows (header row counted)

dr = dt.Rows(i) ' Navigate to visible rows

For Each ccs In ts.GridColumnStyles

If Not ccs.Width = 0 Then

sz = gr.MeasureString(dr(ccs.MappingName).ToString, dgData.Font) ' Measure "width" of data value

ccs.Width = Math.Max(sz.Width + sngPadding, ccs.Width) ' keep using whichever is the larger

End If

Next ccs

Next i

Catch ex As Exception

MsgBox(ex.ToString)

Finally

dgData.EndInit() ' complete graphics edit

gr.Dispose() ' Release graphic object

End Try

End If

End Sub

#3

'Module Created by : Dino Scarcella

'Date : 23-04-2004

'Reason : Needed a module to perform certain functions on datagrids

' parameter 1 : the datagrid you want to autosize

' parameter 2 : the name of the table in your dataset, if this is not right an error will occur TIP: use the name of the dataset table eg. "oDataset.Tables(0).TableName"

' parameter 3 : the padding used when you measure a string in a cell (I use 1 as a default)

' I am just submitting a minor rework of the above authors code.

'Fix: Resize now does throw exceptions on hitting columns that are in the datasoource, but not in TableStyle

'Fix: Resize ignores columns set to 0 width - I am assuming those are intentionally hidden

'Note: Code is streamlined by directly using each GridColumnStyle to loop with

Public Sub AutoSizeDataGridColumns(ByVal dgData As DataGrid, ByVal strMappingName As String, ByVal intPadding As Integer)

Dim ex1 As New Exception("DataGrid must be databound")

Dim ex2 As New Exception("DataSource must be DataTable")

Dim ex3 As New Exception("Invalid Column Name")

Dim dt As DataTable

Dim dr As DataRow

Dim dcCol As DataColumn

Dim gr As Graphics

Dim sngGridNameWidth As Single, sngDataWidth As Single

Dim i As Integer, sngPadding As Single

If dgData.DataSource Is Nothing Then

Throw ex1

Exit Sub

End If

Dim ts As New DataGridTableStyle ' Create a new table style

ts.MappingName = strMappingName ' Address the TableGrid Style

If dgData.TableStyles.Contains(ts.MappingName) = False Then dgData.TableStyles.Add(ts) ' Attach the new style if the table does not have this style

If TypeOf dgData.DataSource Is DataSet Then

dt = CType(dgData.DataSource, DataSet).Tables(dgData.DataMember)

ElseIf TypeOf dgData.DataSource Is DataTable Then

dt = dgData.DataSource

Else

Throw ex2

Exit Sub

End If

If dt.Rows.Count > 0 Then ' Don't change size if there are no rows.

Try

dgData.BeginInit()

dr = dt.Rows(0)

Dim sz As SizeF 'Stores an ordered pair of floating-point numbers, typically the width and height of a rectangle.

gr = dgData.CreateGraphics

sz = gr.MeasureString(New String("M", intPadding), dgData.Font) ' Pad "n" M-width characters

sngPadding = sz.Width

ts = dgData.TableStyles(ts.MappingName)

Dim ccs As DataGridColumnStyle

'Do the headers first

For Each ccs In ts.GridColumnStyles

If Not ccs.Width = 0 Then 'Don't size ones that are intentionally hidden

' Measure the "width" of the text in each grid column header

Try

sz = gr.MeasureString(ccs.HeaderText, dgData.Font)

ccs.Width = sz.Width + sngPadding

Catch ex As ArgumentOutOfRangeException

Throw ex3

End Try

End If

Next

For i = 0 To dgData.VisibleRowCount - 2 ' Calculate width on all visible rows (header row counted)

dr = dt.Rows(i) ' Navigate to visible rows

For Each ccs In ts.GridColumnStyles

If Not ccs.Width = 0 Then

sz = gr.MeasureString(dr(ccs.MappingName).ToString, dgData.Font) ' Measure "width" of data value

ccs.Width = Math.Max(sz.Width + sngPadding, ccs.Width) ' keep using whichever is the larger

End If

Next ccs

Next i

Catch ex As Exception

MsgBox(ex.ToString)

Finally

dgData.EndInit() ' complete graphics edit

gr.Dispose() ' Release graphic object

End Try

End If

End Sub

هذا الموضوع مغلق.

مواضيع مشابهة