473,386 Members | 1,741 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Alternative way of referencing a column from another column in datatable

I have a data table, i want to add a new column on that table, the data in that new column will depend in the data from another column. Now i can think of two ways to accomplish this, first is loop through the whole table and do the statements to get my new columns appropriate data. the second approach would be to give the new column an expression, then convert the table to xml (to remove the expression) then convert the xml string back to datatable.

Now, is there any other way around this? what could be the most optimized way in solving this task?
Feb 6 '12 #1
6 1823
Rabbit
12,516 Expert Mod 8TB
Why not calculate the value in real time instead of storing it?
Feb 6 '12 #2
Hi rabbit,

I forgot to include, i am adding the computed column to polish my table and make it more readable for users(e.g. when data in existing column = "1" then data on the added column should be "COMPLETE"), because i am exporting that table as an Excel File. Which is also the reason why i need to compute for that added column as a whole and not one at a time. Thanks
Feb 6 '12 #3
Rabbit
12,516 Expert Mod 8TB
Again, there's not reason not to calculate it as needed in real time instead of storing it. Or, an alternative approach is to create a "lookup" table and join to that.
Feb 6 '12 #4
Hi rabbit,

here's the scenario, the user would search for a category, then the result would be bound into a repeater. Now that the user has the whole resultset in hand, I want to give them the option to export the result to excel without having to deal with reports. Before i convert the datatable to excel, i would like first to polish it, and make every record readable for users, meaning i have to do this all at the same time. That's why i added a new column with expression referencing another column, i convert the datatable to xml to make it flat data, so i can delete the referenced column, to leave only the new column which is readable for the user. i have also tried looping the whole table and do the statements row per row (to avoid expressions), but i think this is more costly. Now joining to a lookup table seems a good idea, i think i want to try it. Thanks
Feb 7 '12 #5
Rabbit
12,516 Expert Mod 8TB
No problem. Let us know if you have trouble setting up the table.
Feb 7 '12 #6
Hi Rabbit,

i can get a linq with 2 datatables:

Expand|Select|Wrap|Line Numbers
  1. Dim newadtbl As New DataTable
  2.  
  3.     If adtbl IsNot Nothing Then
  4.  
  5.         Dim newatblVal =
  6.  
  7.             From new_col In adtbl.AsEnumerable() _
  8.  
  9.             From old_col In adt.AsEnumerable() _
  10.  
  11.                 .Where(Function(oc) new_col.Item(4).Equals(oc.Item(4)) _
  12.  
  13.                             AndAlso new_col.Item(0).Equals(oc.Item(0))) _
  14.  
  15.                 .DefaultIfEmpty() _
  16.  
  17.             Select TryCast(IIf(old_col Is Nothing, new_col, old_col), DataRow)
  18.  
  19.             newadtbl = newatblVal.CopyToDataTable
  20.  
  21.     Else
  22.  
  23.         newadtbl = adtbl
  24.  
  25.     End If
  26.  
  27.     Return newadtbl
  28.  
  29.  
Now, i wonder how to do this in three datatables, im not sure of the function(oc) part. Your help is greatly appreciated. Thanks
Feb 7 '12 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Xarky | last post by:
Hi, I am making a query to a database, that is returning a DataSet. The DataSet contains one table, columns and rows of data. Now, for that same table, I need to add a new column(Column New)....
2
by: ad | last post by:
I want to add a Column to a DataTable. I use the codes below; I want the Column dc to be a type of Boolean. How can I assign the DataType? ...
1
by: Geoff Jones | last post by:
Hi Is it possible to change the type of data a column holds in DataTable at runtime? For example, suppose that the table column originally holds Strings, can we, at runtime, change it to...
5
by: Tim | last post by:
Hi, I am trying to move a datacolumn in a datatable. What I have tried so far is to create a new datacolumn set it equal to the one I want to remove and then remove it, add another column and...
1
by: johnb41 | last post by:
You can ADD a column to a datatable easily: datatable.Columns.Add() I would like to INSERT a column instead of adding it to the end. Is this possible? Thanks! John
2
by: jvb | last post by:
I am trying to find a way, other than iterating through every row or requerring the database, to get the distinct values from a column in a datatable. Any help would be appreciated. Thanks.
2
by: wapsiii | last post by:
How do I query the datatype of a column in a datatable (Ineed to find the smalldatetime datatypes in a ms sql produced datatable). I'm trying this, but is isn't quite right: For i As Byte = 0...
5
by: GG | last post by:
I am trying to add a nullable datetime column to a datatable fails. I am getting exception DataSet does not support System.Nullable<>. None of these works dtSearchFromData.Columns.Add( new...
13
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I want to create a new column in a datatable from two existing columns. I have no problem to create the new column using the datatable.columns.add method. The problem is the value of the new...
2
by: anatanat | last post by:
hello :) I want to bind a combobox to a specific column in a DataTable. I want the combobox items to display distinct values. I tried: combo.datasource = dataTable; combo.DisplayMember =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.