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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fill dataset with calculated values 4

Status
Not open for further replies.

chilly442

Technical User
Jun 25, 2008
151
US
I need to fill a dataset with values that are calculated in VB code. Is this even possible? OR do you have to run a query to fill a dataset?

Any help is worth a star!
Thanks,
Chilly442
 
A dataset is a collection of tables. What you want to do is create a datatable. You can manually add columns and data to it. You can also add the datatable to a datset if you wanted to.
 
I have two arrays, x and y. x needs to go into the first column(0), and y needs to go into the second(1).
Dim x(3) As String : x(0) = "2006" : x(1) = "2007" : x(2) = "2008" : x(3) = "2009"
Dim y(3) As String : y(0) = "1111" : y(1) = "2222" : y(2) = "3333" : y(3) = "4444"

How would I add this to the dataset?
Thanks for the help!

Chilly442
 
Create a datatable with 2 columns. Then loop through each array.
As you loop through x() add to col1
As you loop through y() add to col2
 
I have found pocos are much more expressive than datatables.
Code:
public class Person
{
   public Guid id {get;set;}
   public string name {get;set;}
   //other properties

   public void send_email()
   {
       //logic to send email
   }

   public void address(Envelope envelope)
   {
       //logic to send snail mail
   }
}
consuming a Person object has more meaning than consuming a generic datatable with strings for columns. Strongly Typed datasets can reduce the strings, but then you still have to deal with all the bloat of the base dataset/table members.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
The problem that I have now is how to tell the y loop to go to the second column?

Here is what I have so far:
Dim x(3) As String : x(0) = "2006" : x(1) = "2007" : x(2) = "2008" : x(3) = "2009"
Dim y(3) As String : y(0) = "1111" : y(1) = "2222" : y(2) = "3333" : y(3) = "4444"

Dim ds As New DataSet
Dim dt As DataTable = New DataTable()

ds.Tables.Add("table1")
ds.Tables(0).Columns.Add("From")
ds.Tables(0).Columns.Add("Total Lost MWh")

For i As Integer = 0 To 3
ds.Tables(0).Rows.Add(x(i))
Next

GridView2.DataSource = ds
GridView2.DataBind()

Thanks for the help guys. It is greatly appreciated.
Chilly442
 
try:
Code:
        Dim x(3) As String : x(0) = "2006" : x(1) = "2007" : x(2) = "2008" : x(3) = "2009"
        Dim y(3) As String : y(0) = "1111" : y(1) = "2222" : y(2) = "3333" : y(3) = "4444"

        Dim i As Integer

        Dim myTable As New DataTable()
        Dim dc1 As New DataColumn("column1", Type.GetType("System.Int32"))
        Dim dc2 As New DataColumn("column2", Type.GetType("System.Int32"))

        myTable.Columns.Add(dc1)
        myTable.Columns.Add(dc2)

        Dim myRow As DataRow
        For i = 0 To x.Length - 1
            myRow = myTable.NewRow
            myRow(dc1) = x(i)
            myRow(dc2) = y(i)

            myTable.Rows.Add(myRow)
        Next
 
You always have the answer.

Thank you soo much for the help!

Chilly442
 
I am using the code that jbenson001 provided above. Now I need to add a row that has only one column across the top, and then go into jbenson001's code.

To look like this:
------------------------------------------
Unit/Plant Number
------------------------------------------
Year | MWh
------------------------------------------
2005 | 2000
2006 | 3000
2007 | 4000
2008 | 5000
2009 | 6000

I can't get the code to allow me to just put one row and column in as a header, then go into the rest of the code. I get an error saying that I need to specify more than one column.

Any help is worth a star!!!


Thanks,
Chilly442
---------------------------------------
If I lived anywhere else I'd be Sunny442
 
a datatable holds data and has no concept of presentation. what you're describing is a presentation concern this is only an issue when you are rendering html.

it can be easily solved with a repeater control and defining the header template
Code:
<asp:repeater>
   <headertemplate>
        <table>
            <thead>
               <tr><td colspan="2"> Unit/Plant Number</td></tr>
               <tr><td>Year</td><td>MWh</td></tr>
            </thead>
            <tbody>
   </headertemplate>
   <footertemplate>
            </tbody>
        </table>
   </footertemplate>
   <itemtemplate>
        <tr>
            <td><%#Eval("column1")%></td>
            <td><%#Eval("column2")%></td>
        </tr>
   </itemtemplate>
</asp:repeater>

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Thank you jmeckley for posting so quickly.

I am binding the datatable to a gridview that has no columns defined on the front end. I do all the creation and population in the code behind. How would I integrate what you have above into that?
Code:
<asp:GridView 
       ID="GridView2" 
       runat="server" 
       Font-Size="X-Small" 
       Font-Names="Arial"
       AutoGenerateColumns="true" 
       OnRowEditing="GridView1_RowEditing" 
       OnRowUpdating="GridView1_RowUpdating" 
       OnRowCancelingEdit="GridView1_RowCancelingEdit" 
       OnRowDataBound="GridView2_RowDataBound" 
       BackColor="Silver" 
       BorderColor="Black" 
       BorderStyle="Solid"  
       ForeColor="Black">
      </asp:GridView>



Thanks,
Chilly442
---------------------------------------
If I lived anywhere else I'd be Sunny442
 
I wouldn't that approach at all.

1. I wouldn't use a gridview, I would use a repeater for more control over the html.
2. I wouldn't create an open ended layout. I would create a specific layout as shown above.

If I had the requirement to change the layout. Then i would create a user control for each presentation layout and load the user requested layout at runtime. I end with many more classes/files/templates, but each template has a specific function and it's much easier to test/debug.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top