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

Resize Datagrid Control's Columns - Vb.net

مغلق
بدأه صايق ضدري في 30 سبتمبر 2007 · 4 رد · 1,234 مشاهدة · في ADO.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

My question is if it's possible to adjust DataGrid control's columns' width

by code (runtime).

I have a DataGrid control that displays table from dataset that contains two

fields. The first one is just an ID, which is 4 digits long, while the other

field is a long text field. I don't want the user to change it every time

when the form loads, but do it for him by code.

Any idea will be thankful!

#2

You can use the following line of code to auto-resize the columns of a DataGridView component

DataGridView1.AutoResizeColumns()

Attached is an example

DataGridAutoResizing1.zip

In order to understand recursion, one must first understand recursion

alawfiadel@yahoo.com

#3

Thank's Adel_N ,But i can't open (DataGridAutoResizing1.zip) because the version of Visual Studio in My PC is 2003 Can You Help Me ?And Thank's Again.DataGridView1.AutoResizeColumns()this code not work in 2003ERROR

#4

I found the solution

...

Private Sub SetColumnProperties(ByVal objDataGrid As DataGrid, ByVal TableName As String, ByVal ColumnIndex As Int16, ByVal ColumnName As String, ByVal ColumnWidth As Integer, Optional ByVal Alignment As HorizontalAlignment = HorizontalAlignment.Left)

Dim TableStyle As DataGridTableStyle

'If the table style is not already created for

'the specified table

' 1. Create a new one

' 2. Assign the Mapping Name

' 3. Add TableStyle to DataGrid

If objDataGrid.TableStyles.Item(TableName) Is Nothing Then

TableStyle = New DataGridTableStyle

TableStyle.MappingName = TableName

objDataGrid.TableStyles.Add(TableStyle)

End If

' Set the properties for the specified column

objDataGrid.TableStyles(TableName).GridColumnStyles(ColumnIndex).Width = ColumnWidth

objDataGrid.TableStyles(TableName).GridColumnStyles(ColumnIndex).HeaderText = ColumnName

objDataGrid.TableStyles(TableName).GridColumnStyles(ColumnIndex).Alignment = Alignment

End Sub

...

then

dim DgSql as string

Dgsql="select rr as fullname from paid_print "

Dim Dgstradp As New SqlClient.SqlDataAdapter(DgSql, strconn)

Dim DGstrda As New DataSet

Dgstradp.Fill(DGstrda, "paid_print")

Me.DataGrid1.DataSource = DGstrda.Tables("paid_print")

SetColumnProperties(DataGrid1, "paid_print", 0, "rr", 150, HorizontalAlignment.Center)

....

And Thank's For all

#5
صايق ضدري كتب:
I found the solution

Looking at that ton of code, I bet you have not found the solution yet!

What is all this man?! You only need to resize the columns of a DataGridView control to their most suitable sizes. Look again to find a ONE line that does it. I never used VS 2003 and can't help now.

Wish you the best.

تم تعديل هذه المشاركة بواسطة Adel_N في 1 أكتوبر 2007 في 04:56

In order to understand recursion, one must first understand recursion

alawfiadel@yahoo.com

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

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