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!

Specifying column widths for more than one DataGrid 1

Status
Not open for further replies.

sard0nicpan

Programmer
Dec 28, 2004
57
US
I am able to set custom column styles for one datagrid, however, I cannot make it work for more than one. The code that works for datagrid1 is below:

Code:
Function MakeItWork1(ByVal mdst As DataSet)

        'Apply new TableStyles to DataGrid 
        Dim dgStyle1 As New DataGridTableStyle
        dgStyle1.MappingName = mdst.Tables(0).ToString

        'First Column (UBID field in my table) 
        Dim dgTextCol As New DataGridTextBoxColumn
        dgTextCol.MappingName = "UBID" 'Name of field in the table 
        dgTextCol.HeaderText = "UBID"
        dgTextCol.Width = 50

        'Add dgTextCol text box column to dgStyle table style 
        dgStyle1.GridColumnStyles.Add(dgTextCol)

        'Second Column (Batch field in my table) 
        dgTextCol = New DataGridTextBoxColumn
        dgTextCol.MappingName = "Batch Name"
        dgTextCol.HeaderText = "Batch Name"
        dgTextCol.Width = 100
        dgStyle1.GridColumnStyles.Add(dgTextCol)

        'Third Column (Date field in my table) 
        dgTextCol = New DataGridTextBoxColumn
        dgTextCol.MappingName = "Date Scanned"
        dgTextCol.HeaderText = "Date Scanned"
        dgTextCol.Width = 120
        dgStyle1.GridColumnStyles.Add(dgTextCol)
        '

        'FourthColumn (Upload field in my table) 
        dgTextCol = New DataGridTextBoxColumn
        dgTextCol.MappingName = "Uploaded"
        dgTextCol.HeaderText = "Uploaded"
        dgTextCol.Width = 150
        dgStyle1.GridColumnStyles.Add(dgTextCol)

        'Add the dgStyle style to the DataGridTableStyle 
        DataGrid1.TableStyles.Add(dgStyle1)


    End Function

I call the Fx with the lines from the following subRoutine:

Code:
            Me.DataGrid1.DataSource = grst.Tables(0)
            Call MakeItWork1(grst)

So OK, I call my second DataGrid(2) in much the same way:
Code:
            Me.DataGrid2.DataSource = grst.Tables(0)
            Call MakeItWork1(grst)

And I duplicate the second "MakeITWork2" Function:

Code:
 Function MakeItWork2(ByVal mdst As DataSet)

        'Apply new TableStyles to DataGrid 
        Dim dgStyle2 As New DataGridTableStyle
        dgStyle2.MappingName = mdst.Tables(0)  'POINT OF ERROR:THE DATA GRID STYLES COLLECTION ALREADY CONTAINS A TABLE STYLE WITH THE SAME MAPPING NAME!

        'First Column (Index field in my table) 
        Dim dgTextCol As New DataGridTextBoxColumn
        dgTextCol.MappingName = "Index" 'Name of field in the table 
        dgTextCol.HeaderText = "Voucher #"
        dgTextCol.Width = 75 

' etc and so on

So I figure, well Tables(0) is for DataGrid1, so maybe Tables(1) is for DataGrid(2):

Code:
         dgStyle2.MappingName = mdst.Tables(1) ' ERROR: CANNOT FIND TABLE . . .

Well as you can tell, I am being overly logical here. So how do I create a new table, and bind the DataGrid2 , 3, & 4 data to it? I am more used to VBA now than anything else. Where did I go wrong?

Thanks in advance,

SP



 
If accessing mdst.tables(1) generates an error, then you dont have 2 tables in your dataset.

Put a break point in the relevant place and check the value of mdst.tables.count.

If you've still got problems, can you repost code with how you are building your dataset




Sweep
...if it works dont mess with it
 
OK, I guess my problem is fundamentally that I fail to create more than one table (Table Count = 0). As per your request Sweep, here is how I build my first two datasets:

Code:
    Function DataLink1(ByVal strCommand As String, Optional ByVal UBIDHold As Integer = 0)
        Dim strTemp As String
        Dim grst As DataSet

        Try

            cnn.ConnectionString = "Server=WTSqlServ;Database=Imagio;Integrated Security=SSPI"
            cnn.Open()

            '1-CREATE COMMAND TEXT; 2-MAKE COMMAND CONNECTION; 3-OPEN DATA ADAPTER
            cmd.CommandText = strCommand '1
            cmd.Connection = cnn '2
            MyDataAdapter.SelectCommand = cmd '3

            '1-CREATE DATASET; 2-FILL UP THE DATA ADAPTER; 3-FILL UP THE DATAGRID
            grst = New DataSet("BatchHistory") '1
            MyDataAdapter.Fill(grst)
            Me.DataGrid1.DataSource = grst.Tables(0)

            Call MakeItWork1(grst)
            cnn.Close()

        Catch oEx As Exception
            Call MsgBox(oEx.Message)
        End Try

    End Function

    Function DataLink2(ByVal strCommand As String)
        Dim strTemp As String
        Dim grst As DataSet

        Try
            cnn.ConnectionString = "Server=WTSqlServ;Database=Imagio;Integrated Security=SSPI"
            cnn.Open()

            '1-CREATE COMMAND TEXT; 2-MAKE COMMAND CONNECTION; 3-OPEN DATA ADAPTER
            cmd.CommandText = strCommand '1
            cmd.Connection = cnn '2
            MyDataAdapter.SelectCommand = cmd '3

            '1-CREATE DATASET; 2-FILL UP THE DATA ADAPTER; 3-FILL UP THE DATAGRID
            grst = New DataSet("VoucherList") '1
            MyDataAdapter.Fill(grst)
            Me.DataGrid2.DataSource = grst.Tables(0)

            Call MakeItWork2(grst)
            cnn.Close()





        Catch oEx As Exception
            Call MsgBox(oEx.Message)
        End Try





    End Function

In each case I am calling a stored procedure from a Sql Server. I make the function calls on loading of the form:

Code:
Public Class Form1

    Inherits System.Windows.Forms.Form
    Dim cnn As New SqlClient.SqlConnection
    Dim cmd As New SqlClient.SqlCommand
    Dim MyDataAdapter As New SqlClient.SqlDataAdapter
    Dim strParam As String

'---------------------------------------------------------

  Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strCommand As String
        Dim row As Integer
        Dim Ubid As Integer


        Try

            strCommand = "spGetRevenueStats"
            Call DataLink1(strCommand)

            row = Me.DataGrid1.CurrentCell.RowNumber
            DataGrid1.CurrentCell = New DataGridCell(row, 1)

            Ubid = Me.DataGrid1.Item(row, 0)
            strCommand = "Exec spGetVouch " & Ubid
            Call DataLink2(strCommand)

            strCommand = "spAllScannerStats"
            Call DataLink3(strCommand)

        Catch oEx As Exception
            Call MsgBox(oEx.Message)
        End Try

    End Sub

So basically what I have is a console to monitor scanning production of our image scanners. Really, its just an exercise for me as I attempt to migrate from VBA/VB6 over to VB.Net (Yes, I'm a Newbie). You can probably tell that I should be able to condense my dataset functions (DataLink1 and DataLink2) to just one function, given that only the datagrid name is different between the two functions. However, I'll keep it separate until I solve this issue. Any guidance is appreciated.

Thanks, SP

 
Ahhhhhhhh . . . resolved with this added to dataset call (typical newbie omission I bet,lol)

Code:
Dim grst As DataSet = New DataSet
Dim NextTable As DataTable = grst.Tables.Add()

Sweep, actually you got me thinking in the right direction . . .

SP
 
sardOnicpan, thank you for posting this question. I have been looking for a 'newbie' explanation on how to set varying column widths in a datagrid. Thanks to your post, I am now able to do that.

I am now getting the "THE DATA GRID STYLES COLLECTION ALREADY CONTAINS A TABLE STYLE WITH THE SAME MAPPING NAME!" error when trying to reload the datagrid with new information. In other words, I have data that occurs between a start date and end date. It works perfect the first time. Then if I change one of the dates and click the button that holds the code I get the error I just mentioned. If you have any suggestions on how to fix that I would greatly appreciate it.

The line of code that it stops at is:

grdFollowUp.TableStyles.Add(dgStyle1)

I'm not sure if that is enough info. Either way, thanks for what you've already helped me with.

Rudy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top