datarow

Finding null value in Dataset - DataRow.IsNull method vs ==DbNull.Value - c#

主宰稳场 提交于 2019-11-30 01:06:08
问题 What are the benefits of using the c# method DataRow.IsNull to determine a null value over checking if the row equals DbNull.value? if(ds.Tables[0].Rows[0].IsNull("ROWNAME")) {do stuff} vs if(ds.Tables[0].Rows[0]["ROWNAME"] == DbNull.value) {do stuff} 回答1: There is no real practical benefit. Use whichever one seems more readable to you. As to the particular differences between them, the basic answer is that IsNull queries the null state for a particular record within a column. Using == DBNull

ADO.NET DataRow - check for column existence

Deadly 提交于 2019-11-29 21:58:56
How do I check for the existence of a column in a datarow? I'm building datatables to organize some data that I've already pulled back from the database. Depending on the type of data in each row, I need to create a datatable with different columns. Then, later on, I want to check and see if the datatable I am looking at has a certain column. I know I can catch the exception and handle it that way, but I'm curious if there is a property or method on the datarow object that will do this for me? Here's how I can do it by catching the exception: public static String CheckEmptyDataRowItem(DataRow

How to extend DataRow and DataTable in C# with additional properties and methods?

空扰寡人 提交于 2019-11-29 15:01:53
问题 I would like create a custom DataRow that will have -let's say- a propery called IsCheapest. public class RateDataRow : DataRow { protected internal RateDataRow(DataRowBuilder builder) : base(builder) { } public bool IsCheapest { get; set ;} } And I want to have a new DataTable that contains only ***RateDataRow***s so that .NewDataRow() returns RateDataRow instance as a new row. what should be the implementation on the class that extends DataTable? Thanks, 回答1: From your question it's not

How to convert a List into DataTable

吃可爱长大的小学妹 提交于 2019-11-29 12:37:33
I'm getting values from another data table as input to list. Now i need to save those list values into another DataTable. List: List<DataRow> list = slectedFieldsTable.AsEnumerable().ToList(); foreach (DataRow dr in slectedFieldsTable.Rows) { list.Add(dr); } New Data table : DataRow newRow = tempTable.NewRow(); newRow["Field Name"] = fieldLabel; newRow["Field Type"] = fieldType; for(int gg =0 ; gg<list.Count; gg++) { tempTable.Rows.Add(????); } I'm stuck here in adding up rows in to new data table. public static DataTable ToDataTable<T>(List<T> items) { DataTable dataTable = new DataTable

Error in datarow,Collection was modified; enumeration operation might not execute [duplicate]

蹲街弑〆低调 提交于 2019-11-29 10:28:05
This question already has an answer here: How do I loop through items in a list box and then remove those item? 8 answers I have for-each loop in which the data row is updated so the exception , Collection was modified; enumeration operation might not execute is generated. any way to fix it? i have seen To-List function but it is not working with data row , here is my code: foreach (DataRow row in dataTable.Rows) { temp = row[0].ToString(); foreach (DataRow rows in dataTable.Rows) { if (temp == rows[0].ToString()) { tempdatatable.Rows.Add(row[0],row[1]); dataTable.Rows.Remove(rows); //Update

C# DataRow Empty-check

我是研究僧i 提交于 2019-11-28 23:16:14
I got this: DataTable dtEntity = CreateDataTable(); drEntity = dtEntity.NewRow(); Then I add data to the row (or not). Lots of code, really don't know if there's anything inside the row. Depends on the input (i am importing from some files). I'd like to do something like: if (drEntity`s EVERY CELL IS NOT EMPTY) { dtEntity.Rows.Add(drEntity); } else { //don't add, will create a new one (drEntity = dtEntity.NewRow();) } Is there some nice way to check if the DataRow's every cell is empty? Or I should foreach, and check them one by one? A simple method along the lines of: bool AreAllColumnsEmpty

Safely Removing DataRow In ForEach

☆樱花仙子☆ 提交于 2019-11-28 20:06:33
I don't understand why this code does not work. foreach (DataRow dataRow in dataTable.Rows) { if (true) { dataRow.Delete(); } } Even though DataRow.Delete doesn't modify the state of the collection, Microsoft documentation states that you shouldn't call it while iterating over the collection: Neither Delete nor Remove should be called in a foreach loop while iterating through a DataRowCollection object. Delete nor Remove modify the state of the collection. The best solution is usually to create a separate collection (e.g. a List<DataRow> ) of items you want to remove, and then remove them

get index of DataTable column with name

怎甘沉沦 提交于 2019-11-28 19:07:13
I have some code which sets the value of cells in a DataRow by column name i.e. row["ColumnName"] = someValue; I want to also set the value for this row in the column immediately to the right of the one found above. Clearly if I was getting the cell by index rather than by column name this would be easy. So is there a way of getting the column index from the column name thus allowing me to do: row[index + 1] = someOtherValue; i.e. do I need create some kind of dictionary of column index and column names when the table is initially created, or can I get the index from the column name later on

How do I get column names to print in this C# program?

﹥>﹥吖頭↗ 提交于 2019-11-28 18:31:53
I've cobbled together a C# program that takes a .csv file and writes it to a DataTable . Using this program, I can loop through each row of the DataTable and print out the information contained in the row. The console output looks like this: --- Row --- Item: 1 Item: 545 Item: 507 Item: 484 Item: 501 I'd like to print the column name beside each value, as well, so that it looks like this: --- Row --- Item: 1 Hour Item: 545 Day1 KW Item: 507 Day2 KW Item: 484 Day3 KW Item: 501 Day4 KW Can someone look at my code and tell me what I can add so that the column names will print? I am very new to C#

Simple way to convert datarow array to datatable

爱⌒轻易说出口 提交于 2019-11-28 16:08:51
I want to convert a DataRow array into DataTable ... What is the simplest way to do this? Why not iterate through your DataRow array and add (using DataRow.ImportRow, if necessary, to get a copy of the DataRow), something like: foreach (DataRow row in rowArray) { dataTable.ImportRow(row); } Make sure your dataTable has the same schema as the DataRows in your DataRow array. joe For .Net Framework 3.5+ DataTable dt = new DataTable(); DataRow[] dr = dt.Select("Your string"); DataTable dt1 = dr.CopyToDataTable(); But if there is no rows in the array, it can cause the errors such as The source