473,396 Members | 2,158 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,396 software developers and data experts.

How to add same value for all rows in a datatable?

65
hi,

i have one datatable with some rows in it.
Now i want to add a new column to it and i want to give same id for all the rows.
i have tried using default value. but it is not working.

How to update the column value with out using loop?


Regards
Veena
Oct 6 '09 #1
5 13270
PRR
750 Expert 512MB
Try this
Expand|Select|Wrap|Line Numbers
  1. DataTable myTable = new DataTable("Bytes");
  2.  
  3.             DataColumn myColumn = new DataColumn();
  4.             myColumn.DataType = System.Type.GetType("System.String");
  5.             myColumn.AllowDBNull = false;
  6.             myColumn.Caption = "Name";
  7.             myColumn.ColumnName = "Name";
  8.             myColumn.DefaultValue = "Sun";
  9.  
  10.             myTable.Columns.Add(myColumn);
  11.  
  12.             DataRow myRow;
  13.             for (int i = 0; i < 10; i++)
  14.             {
  15.                 myRow = myTable.NewRow();  
  16.                 myTable.Rows.Add(myRow);
  17.             }
  18.  
  19.  
  20.             foreach (DataRow dr in myTable.Rows)
  21.             {
  22.                 Console.WriteLine(dr[0].ToString());
  23.             }
  24.  
  25.  
The default value for all rows[0] will be "Sun"
Oct 6 '09 #2
veenna
65
thank u for ur reply.

How can i do this with out using loop?

thanks
veena
Oct 6 '09 #3
PRR
750 Expert 512MB
I have used loop to create rows. You can do away with that
Expand|Select|Wrap|Line Numbers
  1. DataRow myRow = myTable.NewRow();  
  2.                 myTable.Rows.Add(myRow);
  3.  
  4. myRow = myTable.NewRow();  
  5.                 myTable.Rows.Add(myRow);
  6.  
Oct 6 '09 #4
veenna
65
hi prr,

in my datatable rows are there already. so what i am trying to do is just add one column at the last with same value.

if i set default value and i creating new row it is working fine.but it is not adding default value for existing rows.

thanks
veena
Oct 6 '09 #5
PRR
750 Expert 512MB
Could you post some code?
Oct 6 '09 #6

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

Similar topics

9
by: Coleen | last post by:
Hi All :-) I found the way to get my column sum (Thanks Cor I did it a little different, but the result is what I wanted) I used: dt_stat_report_3b.Columns.Add(New DataColumn("Sum",...
2
by: eBob.com | last post by:
I've got the basics of an XML ini file working. I.E. I can stash away and retrieve user preferences. (Code below.) But how do I handle a new preference? Say I have A and B. And then I invent...
0
by: JSantora | last post by:
Essentially, InsertAT is broken! For the past couple of hours, I've been getting this "Parameter name: '-2147483550' is not a valid value for 'index'." error. Apparently, its caused by having...
0
by: Matt | last post by:
I derived my own custom class from the datagrid class. I overrode the ProcessCmdKey Function, like this, to catch the up and down arrow keys: ====== Protected Overrides Function...
3
by: Niyazi | last post by:
Hi all, I have a dataTable that contains nearly 38400 rows. In the dataTable consist of 3 column. column 1 Name: MUHNO column 2 Name: HESNO Column 3 Name: BALANCE Let me give you some...
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
19
by: Dave | last post by:
If Iwant to check if dataset1.SelectQuery1.column1 == System.DBNull.Value. How do I do this? What I wrote above will give an error. -- L. A. Jones
0
by: Maart_newbie | last post by:
Hi all, I've got a question about returning the value of a pk-column to a DataTable after inserting a row (via a data-adapter) using MySql5. Here is the SQL and code concerned: ...
3
by: =?Utf-8?B?UmljaCBIdXRjaGlucw==?= | last post by:
I'm not really sure how to ask this question because I'm still getting my feet wet with data access and VB.NET, but here goes: To start off with, I'm using VB 2005 Express to connect to an Access...
12
by: ive1122 | last post by:
Hi guys, I am looking into create a dynamic survey as posted in Get User Input From Dynamic Controls but with some different environment Below is what i am trying to do: First when the user...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.