datarow

How to write an extension method for DataTable.Rows[i][j]? [closed]

余生长醉 提交于 2019-12-08 12:56:32
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 6 years ago . I would like to create an extension method to be implemented like below (if it is even possible) which would be similar to the .ToString() extension method. Can someone please point me in the right direction. I attempted to search it in Google and cannot locate anything. DataTable table = db

Map RowDataCollection to DTO using AutoMapper

倖福魔咒の 提交于 2019-12-08 07:46:47
问题 Is there a way to map a RowDataCollection to DTO using AutoMapper? Here is a scenario: DataRow to Object user.UserId = row["UserId"]; user.UserName = row["UserName"]; 回答1: glbal.asax configuration Mapper.CreateMap<DataRow, EventCompactViewModel>() .ConvertUsing(x => (EventCompactViewModel)AutomapperExtensions.DataRowMapper(x, typeof(EventCompactViewModel), null)); Data row mapper public static object DataRowMapper(DataRow dataRow, Type type, string prefix) { try { var returnObject = Activator

changing values of a column in datatable in vb.net

孤街醉人 提交于 2019-12-07 23:46:09
问题 I want to loop through a databale and change the values of a specific column in that datatable. eg: the database returns Request_ID , Desc , Date and Status_Ind . The Status_Ind column contains integer values. I dont want to show the integer values to the user. I want to convert that to a string value based on the integer values returned in that columns. if Status_Ind = 1 then 'the values changes to Published 回答1: You could do it by creating another column: Dim dt As New DataTable dt.Columns

C# Reflection: Getting the fields of a DataRow from a Typed DataSet

℡╲_俬逩灬. 提交于 2019-12-07 18:57:43
问题 I am currently building a method that takes an object that is of type DataRow from a typed DataSet, and then returning a string in JSON format of the fields in the DataRow (for use in a Web Service). By using System.Reflection , I am doing something like this : public string getJson(DataRow r) { Type controlType = r.GetType(); PropertyInfo[] props = controlType.GetProperties(); foreach (PropertyInfo controlProperty in props) { } return ""; } And then in the foreach statement, I would iterate

Compare two datarows

风格不统一 提交于 2019-12-07 11:37:33
问题 I have a datatable with multiple rows inside it. I have got 1 more row and I want to check if this row is a duplicate of the existing rows in the datatable. So I tried like: DataTable dataTable = GetTable(); if (dataTable.Rows.Count > 1) { for (int i = 0; i < dataTable.Rows.Count; i++) { var dataRow = dataTable.Rows[i]; if (dt.Rows.Contains(dataRow) && dt.Rows.Count != 0) // Giving error continue; dt.ImportRow(dataRow); return dataRow; } } Here, my dataTable can also be null/empty for the

changing values of a column in datatable in vb.net

风格不统一 提交于 2019-12-06 08:53:27
I want to loop through a databale and change the values of a specific column in that datatable. eg: the database returns Request_ID , Desc , Date and Status_Ind . The Status_Ind column contains integer values. I dont want to show the integer values to the user. I want to convert that to a string value based on the integer values returned in that columns. if Status_Ind = 1 then 'the values changes to Published You could do it by creating another column: Dim dt As New DataTable dt.Columns.Add(New DataColumn("status_int", GetType(Int32))) dt.Columns.Add(New DataColumn("status_str", GetType(String

iterating through rows of a datagrid

拟墨画扇 提交于 2019-12-06 07:43:41
I am trying to extract values from a datagrid, by iterating through all the rows of the datagrid foreach (DataRow drv in PGIPortfolio.Items) { // DataRow row = drv.Row; string acname = drv["Portfolio"].ToString(); string paramt = drv["Par Amount"].ToString(); MessageBox.Show(acname); } But it is giving me an InvalidCastException at DataRow drv. Could someone tell me what changes I should make so it works? The datagrid has a binding, and it is being populated by a stored procedure from ms sql 2008 database Use a DataGridRow not a DataRow they are a different objects foreach (DataGridRow drv in

Trouble with updating DataRow in C#

人走茶凉 提交于 2019-12-05 02:29:24
问题 I have a very simple C# DataTable problem that I cannot seem to wrap my head around; it seems so strait forward but I must be missing something. I was hoping that someone could explain to me why I'm unable update a cell value in a DataTable like shown below: Code: DataTable t = new DataTable(); t.Columns.Add("MyCol"); t.Rows.Add("old value"); t.Rows[0].ItemArray[0] = "new value"; t.AcceptChanges(); dataGridView1.DataSource = t; //did not work. still reads "old value" Any help would be

DataTable - foreach Row, EXCEPT FIRST ONE

◇◆丶佛笑我妖孽 提交于 2019-12-04 18:57:54
问题 I am using a DataTable for some calculations in my app. I need to do the iterate trough all the rows except the first one. Is it possible? Something like: DataTable dt; foreach (DataRow r in dt.Rows /*EXCEPT THE FIRST ONE*/) { //do something... } 回答1: Ok you got your answers but in case you donT want to use linq. Check the index of the row in the table: foreach (DataRow row in m_dtMatrix.Rows) { if (m_dtMatrix.Rows.IndexOf(row) != 0) { ... } } 回答2: LINQ is your friend: DataTable dt; foreach

Use of C# var for implicit typing of System.Data.Datarow

别来无恙 提交于 2019-12-04 16:11:47
问题 foreach (var row in table.Rows) { DoSomethingWith(row); } Assuming that I'm working with a standard System.Data.DataTable (which has a collection of System.Data.DataRow objects), the variable 'row' above resolves as an object type, not a System.Data.DataRow . foreach (DataRow row in table.Rows) { DoSomethingWith(row); } Works as I would expect. Is there a particular reason for this? Thanks. 回答1: That's because Rows is DataRowCollection , which in turn is IEnumerable and not IEnumerable