How to determin if a datarow has already been added to a datatable yet.

  • Thread starter Thread starter moondaddy
  • Start date Start date
M

moondaddy

using .net 3.0 and c#, I have a public datarow from a strongly typed dataset
datatable. the datarow gets created using the method
myTable.NewmyTableRow(), but doesn't get added to the table until later. At
some point later, is there a quick and easy way determine if the datarow has
already been added to the datatable yet?
 
Hi George ,

You may check the DataRow.RowState property. The value of
DataRowState.Detached means that the DataRow is created but is not added
into the DataRowCollection yet, while DataRowState.Added means the row is
already added. See the link below for more details:
http://msdn.microsoft.com/en-us/library/system.data.datarowstate.aspx

As an example, the following code snippet will generate the following
output:

"Detached
Added

Detached
Added

Detached
Added

Detached
Added

Detached
Added
"

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("column1", typeof(int)));
dt.Columns.Add(new DataColumn("column2", typeof(string)));

for (int i = 0; i < 5; i++)
{
DataRow dr = dt.NewRow();
dr["column1"] = i;
dr["column2"] = "item" + i.ToString();

Console.WriteLine(dr.RowState.ToString());

dt.Rows.Add(dr);

Console.WriteLine(dr.RowState.ToString() + Environment.NewLine);
}

Hope it helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top