DataSet into DataGridView

Hi all,

I have the below code I=92m using to populate three DataGridView=92s I
have on my form without much success at the moment as the DataGridView
only displays one column of data once it has run :/

In the example I am just trying to get it working for one DataGridView
with the intention of passing in SQL to define the DataSet=92s once its
working.

I am unsure weather I have missed something because I can view all the
data in the visualiser but it only seems to populate the one column in
the DataGridView.

Please help.

Scott.

    Private Sub cboUserName_SelectedIndexChanged(ByVal sender As
System.Object, _
        ByVal e As System.EventArgs) Handles
cboUserName.SelectedIndexChanged

        Dim lngUserID As Long

        For i As Integer =3D 0 To ALWDBDataSet.Tables
("tblUser").Rows.Count - 1
            If ALWDBDataSet.Tables("tblUser").Rows(i).Item("UserName")
=3D cboUserName.Text.ToUpper Then
                lngUserID =3D ALWDBDataSet.Tables("tblUser").Rows(i).Item
("UserID")
                Exit For
            End If
        Next

        '// Initialize a new instance of the OleDbConnection Class
        objConnection =3D New OleDbConnection(strConncectionString)

        '// Build query string
        Dim strSQL As String =3D "SELECT * FROM tblUserLogs WHERE UserID
=3D " & lngUserID
        '// Initialize a new instance of the OleDbCommand Class
        objCommand =3D New OleDbCommand(strSQL, objConnection)

        Call PopulateGrid(dgvLogInfo, "tblUserLogs")


    End Sub




    Private Sub PopulateGrid(ByVal grd As DataGridView, ByVal strTable
As String)

        '// Initialize a new instance of the OleDataAdapter class
        objDataAdapter =3D New OleDbDataAdapter

        '// Initialize a new instance of the DataSet Class
        objDataSet =3D New DataSet

        '// Set the SelectCommand for the OleDbDataAdapter
        objDataAdapter.SelectCommand =3D objCommand

        Try
            '// Poplate the DataSet
            objDataAdapter.Fill(objDataSet, strTable)

            '// Bind the DataSet to the DataGrid
            grd.DataSource =3D objDataSet
            grd.DataMember =3D strTable

            '// Set the AlternatingRowsDefaultCellStyle.BackColor
property
            grd.AlternatingRowsDefaultCellStyle.BackColor =3D
Color.WhiteSmoke

            '// Set the CellBorderStyle property
            grd.CellBorderStyle =3D DataGridViewCellBorderStyle.None

            '// Set the SelectionMode property
            grd.SelectionMode =3D
DataGridViewSelectionMode.FullRowSelect

            '// Set the AutoSizeColumnsMode property
            'grd.AutoSizeColumnsMode =3D
DataGridViewAutoSizeColumnsMode.AllCells

        Catch OleDbException As OleDbException
            MessageBox.Show(OleDbException.Message)
        End Try

        '// Cleanup
        objCommand.Dispose()
        objCommand =3D Nothing
        objDataAdapter.Dispose()
        objDataAdapter =3D Nothing
        objDataSet.Dispose()
        objDataSet =3D Nothing
        objConnection.Dispose()
        objConnection =3D Nothing

    End Sub
0
Scott
1/7/2010 10:43:53 AM
dotnet.languages.vb 830 articles. 0 followers. Follow

2 Replies
1090 Views

Similar Articles

[PageSpeed] 45

try something like this:

Initially:

dataAdapter1.SelectCommand.CommandText = "Select fld1, fld2, fld3 from
tbl1"
dataAdapter1.Fill(dataset1, "tbla")
datagridview1.datasource = dataset1.Tables("tbla")

Now a new query using a different table and a different number of
fields:

dataset1.Tables.Remove(dataset1.Tables("tbla"))
datagridview1.Columns.Clear

dataAdapter1.SelectCommand.CommandText = "Select fld5, fld6, fld7, fld8,
fld9 from tbl2"
dataAdapter1.Fill(dataset1, "tbla")
datagridview1.datasource = dataset1.Tables("tbla")

It seems like VS remembers the original table it creates and the
original number of columns.  So you have to remove the table from the
dataset and remove the columns from the datagridview each time.  Then
run the new query.



Rich

*** Sent via Developersdex http://www.developersdex.com ***
0
Rich
1/7/2010 5:45:01 PM
On 7 Jan, 17:45, Rich P <rpng...@aol.com> wrote:
> try something like this:
>
> Initially:
>
> dataAdapter1.SelectCommand.CommandText =3D "Select fld1, fld2, fld3 from
> tbl1"
> dataAdapter1.Fill(dataset1, "tbla")
> datagridview1.datasource =3D dataset1.Tables("tbla")
>
> Now a new query using a different table and a different number of
> fields:
>
> dataset1.Tables.Remove(dataset1.Tables("tbla"))
> datagridview1.Columns.Clear
>
> dataAdapter1.SelectCommand.CommandText =3D "Select fld5, fld6, fld7, fld8=
,
> fld9 from tbl2"
> dataAdapter1.Fill(dataset1, "tbla")
> datagridview1.datasource =3D dataset1.Tables("tbla")
>
> It seems like VS remembers the original table it creates and the
> original number of columns. =A0So you have to remove the table from the
> dataset and remove the columns from the datagridview each time. =A0Then
> run the new query.
>
> Rich
>
> *** Sent via Developersdexhttp://www.developersdex.com***

i also added  grd.AutoGenerateColumns =3D True after the clear method
and it seems to be working ok now.

thanks for your help
0
Scott
1/7/2010 8:36:23 PM
Reply:

Similar Artilces:

Automagically create database schema from XML dataset
Hello. I am trying to create a database from an XML file. I am able to create a dataset from the XML doc, but how can I create a database schema from the dataset and populate the database? Or is this even possible using VS.NET 2003? Part of the XML file follows. Thanks for any help. Steven VS.NET 2003 and C# newbie! ----- Part of the XML (one full record across several tables) -------- <!DOCTYPE PubmedArticleSet...> - <PubmedArticleSet> - <PubmedArticle> - <MedlineCitation Owner="NLM" Status="Completed"><PMID>11604973</PMID> - &l...

Creating typed dataset from an industry standard schema
Has anyone run into problems creating typed datasets from complex schemas? I am attempting to create a Typed Dataset in Visual Studio .Net from a .xsd schema file for a industry standard web service. I have run the schema through XML Spy and validated it with no errors, but when attempting to generate a dataset, I keeping getting a 'undefined data type' error. The schema is quite large so I don't know if that is causing the problem or if it is just something odd about .Net's parser. Here is the section of the schema causing the problem. There are only two spots where the...

Need help designing XML Dataset
Hello I developed and tested a web application using VS.NET 2003, VB, .NET Framework 1.1.4322, ASP.NET 1.1.4322 and IIS5.1. It uses a web form. I tried doing this without any help, but i'm getting nowhere. I want to create an XML schema (XSD) for a dataset/datatable using the XML designer. I want to be able to have a table for each trip. The trip will contain a trip no, date etc. But i also want it to have a State element like "PA, NJ, MD" for each state with miles traveled for each state. I don't want to enter 50 elements name MilesPA, MilesNJ, MilesMD etc. Is t...

How to RANDOMLY split a whole dataset into two sub-dataset? #3
Hi, At your possible convenience, might anyone please kindly answer my question? How to RANDOMLY split a whole dataset (n=2000) into two sub dataset (n=1000, n=1000) in SPSS or Excel? Thank you very much. Please take care Caroline You have two answers in your other posting. Pete On Jul 17, 6:33 am, zencaroline <zencarol...@gmail.com> wrote: > Hi, > > At your possible convenience, might anyone please kindly answer my > question? > > How to RANDOMLY split a whole dataset (n=2000) into two sub > dataset (n=1000, n=1000) in SPSS o...

SSRS Linking Dataset
I have converted some reports in Access to SQL 2008 Reporting Services. However, some of the reports have subreports in them and they look fine in design but when I try to run the report previewing it from VS 2008 I get the main report but not the subreport. Apparently, the subreport is not tied to a Dataset even though the dataset shows up in the Report Data section on left side of VStudio. I cannot find anywhere in proerties to attach a dataset to the subreport. Also, how do I "link" the subreport to the main report to get only the correct records in the subrepo...

GetXml from DataSet
Hello, I transfer data from a dataset to an XML-document by using the GetXml-method in VB.NET. I noticed, that for datafields, which are empty (have no content) in the dataset, no elements are created in the XML-document. How can I force to generate elements (with no content) for this kind of datafields ? Thanks em Check out http://www.topxml.com/sqlxml/default.asp It has some excellent information on this. Regards Keith Chadwick "em" <nix@nix.nix> wrote in message news:018f01c3ba37$e4b0b760$a001280a@phx.gbl... > Hello, > I transfer data from a dataset to an XML...

experts: structure of dataset xml
hello, i have a problem, i wonder if anyone can help... i am retrieving records from an access table using a dataset and want the data to be returned in xml, so i am using the dataset.GetXml() method. lets say, for example, that the xml is in the following format: <Users> <User> <UserId>1</UserId> <Username>robert</Username> <Password>friend</Password> </User> <User> <UserId>2</UserId> <Username>natasha</Username> <Password>sunny</Password...

How do I read an XmlDocument object into a dataset???
I need to read an in-memory instance of a loaded XmlDocument into a referenced DataSet. What's the best way to do this? I've tried "myDataSet.ReadXml(myXmlDocument)" but it won't compile. I get a compile error because the XmlDocument type isn't accepted by the ReadXml() method. I need to figure this out! Thanks, ~A~ Blah - nevermind. I don't need the xmldocument - I just feed the xml stream straight into the dataset. Thanks, ~A~ "Kewlpack" wrote: > I need to read an in-memory instance of a loaded XmlDocument into a > referenced DataSet....

xml and dataset
I want to take a dataset and save the data as an xml document. I know how to do this, but I need the xml to be formatted in a certain way. What are my options? I have thought about using a stylesheet to format from xml to xml. I can create an xsd, but that does not seem to save the dataset the way I need it. It just adds the schema to the top, but does not put the xml in a hierarchical format. TypedDataSet.WriteXml("C:\Test.xml", XmlWriteMode.WriteSchema) Hi Ken, You can do it using the XMLdoc. Because I do not know what you are reformating it is difficult to say more. Cor...

DataGridView checkbox.
When I use dataGridView, and one of it's elements is a checkBox. How can I catch the event, exactly when the checkBox is changed. CellValueChanged is not good enough, since the event occurs after both two steps : 1. I change the value of the cell. 2. I move to another cell. I want exactly when the checkBox.checked is changed (DataGridViewCheckBoxCell). Thanks :) On 24/06/2010 11:54, Mr. X. wrote: > When I use dataGridView, and one of it's elements is a checkBox. > How can I catch the event, exactly when the checkBox is changed. > I want exactly when the...

Write dataset to xml and back again to dataset?
Before a delete on any table, I would like to write the contents of the soon-to-be-deleted row to that application's single "graveyard" table (alternate: or document as coded below). SQL Server 2005 solution: I would store the tablename, today's date, username, and the key column values of the soon-to-be deleted row as the composite key of the new graveyard table row. The soon-to-be-deleted row's data would be stored in a single xml-type column. XML File solution: composite key from above would be the name of a filename.xml file. The data from the soon-to-be-deleted...

Lookup in DataGridView? Any Progress? Worth Trying?
In a DataGridView, is it possible to have a column that manages user input by providing a "drop down" list of values the user can choose from, with those values either being "spelled out" in code or read from another db.table.column? If so, does anyone know where I might find instructions on how to set that up? I've seen lots of old posts on this topic, but nothing necessarily specific to the latest incarnation of the DataGrid, the DataGridView. Is it worth trying to pop a List Box on the ColEdit event, let the user "select" from the List Bo...

very large datasets
I have a very large dataset ( 600 MB )that I need to open with excel 2003. Will the system crash if I do this ? Any suggestions, comments welcome. thanks Hi probably you will encounter problems when opening this with Excel: - one worksheet has a maximum of 65536 rows -> so you may have to split your dataset on several worksheets - Depends on the dataset format (text format, Access database, etc.) -> so you may provide some more details IMHO Excel seems not to be the right application to open this kind of large dataset. Have you tried opening this datafile (of course after making a ...

DataSet Data & DataSet Schema
I have created a strongly typed dataset and populated it with data in my data layer. I have confirmed that the DataSet does, in fact contain my data and that there is a schema governing this data (obviously, since it is a strongly typed DataSet). I used the drag & drop method to create the .xsd file. I have created 2 methods in my data layer: one that returns the data in the DataSet (dataset.getxml) and one that returns the schema (dataset.getxmlschema). I have confirmed that both of these xml strings are being returned to the presentation layer. In my presentation layer, I have...

Versioning a DataSet
Hi, I have a DataSet defined in Visual Studio 2008 with an XSD file. The contents are saved to an XML file using DataTable.WriteXml() and read back using DataTable.ReadXml(). Now I am going to have to modify the schema. Specifically I want to replace a boolean field with a (tristate) enumeration. Alternativly, I could leave the existing field and add an additional field. I will want to read files written with the old schema, but will only write the new schema. I have been looking for recommended practices for handling this kind of problem. Although I have found general information ab...

Transform Dataset using XSL in-place?
Hello, I'm trying to apply an XSL to a dataset contents. More specifically, I want to apply an XSL to a dataset or an XmlDataDocument and put the resulting transformed data back into another DataSet or simply to modify the current DataSet. The first part is easy: Dim xmlDataDoc as XmlDataDocument = new XmlDataDocument(oracleDS) Dim xslTrans as XslTransform = new XslTransform xslTrans.Load("stylesheet.xsl") xslTrans.Transform(xmlDataDoc, Nothing.. Stream) Dim resultDS as new DataSet() resultDS.ReadXml(?) The problem becomes that XslTransform.Transform wants to output to a str...

importing dataset beyond 256 columns
Might anyone be aware of a Q article on this issue? Arturo The third hit I got searching on "256" returned this: http://support.microsoft.com/default.aspx?scid=kb;en-us;272729 -- Jim Rech Excel MVP "Arturo" <Arturo@discussions.microsoft.com> wrote in message news:4EAC3053-0079-4DD7-B1B8-F2EFA9BDD21D@microsoft.com... | Might anyone be aware of a Q article on this issue? | | Arturo ...

Generate typed DataSet from XML or create typed DataSet from xsd?
Hi, I can easily create an xsd from an xml file in Visual Studio 2005. But I want a typed DataSet, so is there an option to create that typed DataSet from the xsd or the xml file? I need many such typed DataSets so I don't want to create them from scratch. On a sidenote: I am loading the XML from http, I transform it via xslt and then load the result into a typed DataSet. I think I am doing a lot of inefficient things. I am always having trouble with "how to get a reader / writer here" and "what to pass to this readxml there" so I end up reading stuff into stringbuild...

how to query a dataset with xml tables inside.
This is a multi-part message in MIME format. ------=_NextPart_000_0030_01C3DB37.4468A9E0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable I have two xml docs, authors & articles. Both have authorID as a common = node <?xml version=3D"1.0" encoding=3D"utf-8"?> <!--This is a comment--> <Authors> <Author> <AuthorID>0</AuthorID> <Name>Mark</Name> <Login>mark</Login> <Pwd>markpwd</Pwd> </Author> <Author> <AuthorI...

loading XML into a dataset
I'm currently reading data from an XML file using XPathNavigator and XPathNodeIterator to select the subset of data that I want. I've never using the XPath objects before so I'm not sure how to get this data into a dataset. XPathDocument doc = new XPathDocument(@"C:\inetpub\wwwroot\test\test.xml"); XPathNavigator nav = doc.CreateNavigator(); XPathNodeIterator iter = nav.Select("/test/translation/engword[preceding-sibling::fraword = '" + strFraWord + "']"); Can anybody help me out with this? Any assistance would be re...

How to compare two dataset?
In myapp, I can create two datasets: [dstServer1] and [dstServer2]. They are coming from two tables which are in two different SQL server: [Order1] (in server1) and [Order2] (in server2). These two tables have the same data structure. How to code to compare two dataset and then insert difference into another table without using linked server? -- Message posted via SQLMonster.com http://www.sqlmonster.com/Uwe/Forums.aspx/sql-server/201004/1 VB.NET/C# do not have such possibility? Loop thru the sets and compare them on the client "aspfun via SQLMonster.com" &l...

DataSet or Classes
I must work with the xml files. Load data from xml, change it, save it. What is the best idea? To use ADO.NET, I mean to load data from xml to DataSet, or create classes and work with these classes (something common with "Castor"). Thanks, Ruslan Ruslan, It depends. Are you familiar with DataSets and does your data map easily to them? If so, use the DataSet.It's a great interface. However, it won't handle all XML data structures. A good alternative is the XML DOM. Kathleen "Ruslan" <ruslan_albu@hotmail.com> wrote in message news:ugTVDG5SDHA....

Datagridview how to view a property who is a class
Hi, I use a datagridview, the data source is a bindinglistof(Myclass). Myclass have a two property wich are a class (Myclass2, Myclass3). When I run, it show me two colums named "myproject.class2", "myproject.class3" I want to obtain two colums wich are Myclass.Myclass2.myproperty Myclass.Myclass3.myproperty How to do this ? Thanks in advance ...

About a DataSet
Hello! A simple question is there TableAdapter within a DataSet ? //Tony "Tony Johansson" <johansson.andersson@telia.com> wrote in message news:ZAyOm.12984$U5.192909@newsb.telia.net... > A simple question is there TableAdapter within a DataSet ? http://msdn.microsoft.com/en-us/library/system.data.dataset.aspx -- Mark Rae ASP.NET MVP http://www.markrae.net "Tony Johansson" <johansson.andersson@telia.com> wrote in news:ZAyOm.12984 $U5.192909@newsb.telia.net: > A simple question is there TableAdapter within a DataSet ? ...

Read XML into Dataset and load SQL server table from Dataset
Hello all, I am working on a project with the following characteristics: 1. Load data from a SQL server table to an xml file 2. Read the xml file into a dataset. 3. Load data from the dataset into another SQL server table. I was able to accomplish the first objective. I used the following code to read the generated xml file into a dataset. Dim strXml As String = "C:\Customers.xml" Dim sr As StreamReader = New StreamReader(strXml) Dim ds As DataSet = New DataSet ds.ReadXml(sr, XmlReadMode.IgnoreSchema) How can I traverse in the dataset and post each row to my desired table on ...