Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid view Columns not sizable after fill

Status
Not open for further replies.

abaldwin

Programmer
Mar 27, 2001
798
US
VB.net 2005
Windows application.
1 form containing a datagridview

Using the following code I am filling a grid with data.

Dim con As New SqlConnection
Dim stSql As String = ""
con = New SqlConnection("Data Source=" & pstServer & ";" & _
"Initial Catalog=" & pstDb & ";User Id=" & pstUser & ";Password=" & pstPass & ";")
con.Open()
Dim myds As DataSet = New DataSet

stSql = "SELECT SFORDFIL_SQL.ord_loc, IMITMIDX_SQL.prod_cat, SFORDFIL_SQL.byr_plnr, SFORDFIL_SQL.item_no, IMITMIDX_SQL.item_desc_1, " & _
"SFORDFIL_SQL.ord_no, SFORDFIL_SQL.ord_qty - SFORDFIL_SQL.qty_complete AS QtyRem, SFORDFIL_SQL.ord_status, SFORDFIL_SQL.sch_meth, " & _
"CONVERT(smalldatetime, CONVERT(varchar(8), SFORDFIL_SQL.due_dt)) AS due_dt, " & _
"CONVERT(smalldatetime, CONVERT(varchar(8), SFORDFIL_SQL.start_dt)) AS start_dt " & _
"FROM SFORDFIL_SQL INNER JOIN IMITMIDX_SQL ON SFORDFIL_SQL.item_no = IMITMIDX_SQL.item_no " & _
"WHERE (SFORDFIL_SQL.due_dt <> 0) AND (SFORDFIL_SQL.start_dt <> 0) AND (SFORDFIL_SQL.ord_status <> 'C')"

Dim myadapter As SqlDataAdapter = New SqlDataAdapter(stSql, con)
Try
myadapter.Fill(myds, "HeaderTable")
grdOrderHeader.DataSource = myds.Tables("HeaderTable")
FormatGridStyle(grdOrderHeader)

Catch ex As Exception
MsgBox(ex.Message & vbCrLf & vbCrLf & "Check the integrity of the data in SFORDFIL_SQL.")
'Exit Sub
End Try [/qote]

The columns have been predefined in the grid with the autosize and all set to things like columnheader. When I pre-define them like this the columns are no longer sizeable by the user at runtime.

I am new to vb.net. Used vb6 for YEARS just cant seem to figure this one out after Days of searching the web.

Thanks

Andy Baldwin

"Testing is the most overlooked programming language on the books!"

Ask a great question, get a great answer. Ask a vague question, get a vague answer.
Find out how to get great answers FAQ219-2884.
 
Code:
grdOrderHeader.AllowUserToResizeColumn=[blue]True[/blue]

If [blue]you have problems[/blue], I want [green]source code[/green] AND [green]error messages[/green], none of this [red]"there was an error crap"[/red]
 
Tried that. Columns that are predefined in the grid are not resizable. The rows are though.


Andy Baldwin

"Testing is the most overlooked programming language on the books!"

Ask a great question, get a great answer. Ask a vague question, get a vague answer.
Find out how to get great answers FAQ219-2884.
 
Why don't you give your columns friendly names, and then let the datasource "figure" out the column names. I'm lazy like that, and it hasn't bit me yet.

If [blue]you have problems[/blue], I want [green]source code[/green] AND [green]error messages[/green], none of this [red]"there was an error crap"[/red]
 
I've struggled with this same behavior myself. I thought I could bypass it by setting the AutoSizeColumnsMode = AllCells, loading the DataGridView, then setting the AutoSizeColumnsMode to None. The problem with that approach is the column widths were reset also. So I came up with this:
Code:
grdOrderHeader.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells

' Load grdOrderHeader

Call UnlockDataGridViewColumns(grdOrderHeader)

...


Private Sub UnlockDataGridViewColumns(ByRef dgv As DataGridView)
    Dim widths(dgvAssets.Columns.Count) As Integer

    For ix As Integer = 0 To dgv.Columns.Count - 1
        widths(ix) = dgv.Columns(ix).Width
    Next

    dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None

    For ix As Integer = 0 To dgv.Columns.Count - 1
        dgv.Columns(ix).Width = widths(ix)
    Next

End Sub
 
Qik3

Are you suggesting that the returned fields from the sql be aliased to provide the column headers?


Andy Baldwin

"Testing is the most overlooked programming language on the books!"

Ask a great question, get a great answer. Ask a vague question, get a vague answer.
Find out how to get great answers FAQ219-2884.
 
Yes, yes i am.

Select myCol as ImportantHeader

Then when you do your data adapter fill it gens the correct column headings, which show on your datagridview.

Told you, I don't like to write code, so if I can find a way to "automagically" do it, I will.

If [blue]you have problems[/blue], I want [green]source code[/green] AND [green]error messages[/green], none of this [red]"there was an error crap"[/red]
 
Since being VERY new to .net (I know, I waited way to long to move from my trusted VB6 and all). I was trying to predefine the columns in the grid setting the header, dataproperty and formating.
This seemed to allow me to take advantage of the design time settings for wrapping, sizing, and making certain cells read only while allowing user to change only the two cells I want them to change.

The approach with Friendly names as aliases gets me the user sizable grid but then have to write code to keep the readonly = true on 8 of the 10 columns.

Any other thoughts?


Andy Baldwin

"Testing is the most overlooked programming language on the books!"

Ask a great question, get a great answer. Ask a vague question, get a vague answer.
Find out how to get great answers FAQ219-2884.
 
It's interesting that you have only some columns editable.
That may be as good as it gets.

Normally when I have to do this, I make the whole datagrid readonly and then they can select a row, which populates a couple text boxes and they can edit the values from there. It takes more screen space, but I don't have any "auto" update procedures. I don't use the wizard for the datagridview.

If [blue]you have problems[/blue], I want [green]source code[/green] AND [green]error messages[/green], none of this [red]"there was an error crap"[/red]
 
Yea, that was my M.O. for years. Making this app more "EXCELLISH". Most of the cells are for user reference while they can only update 2 of the cells. No autoupdating of records though. Only processing after they commit the changes in the grid.


Andy Baldwin

"Testing is the most overlooked programming language on the books!"

Ask a great question, get a great answer. Ask a vague question, get a vague answer.
Find out how to get great answers FAQ219-2884.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top