Add columnname and data from datatable to a dictionary? C# - c#

I am looping through a datatable and currently I do
string customerId = row["CustomerId"].ToString();
string companyName = row["Company Name"].ToString();
Instead of declaring every variable how do I add these do a dictionary?
I was thinking something like:
foreach (DataRow row in customerTbl.Rows)
{
Dictionary<string, string> customerDictionary = new Dictionary<string, string>();
customerDictionary.Add(row[].ToString(), row[].ToString());
so yeah, how do I get the row name and value into there?
Thanks in advance.
EDIT:
I guess I didn't give you all information about what I am trying to do.
I query a database for a customer and populate a datatable with about 50 columns, I use the customer id to query another database and get the same customer from there. I then want to compare all 50 fields from both databases for the same customer and check if any field is different. I thought I could add the column name with the value in a dictionary for each row for each customer and then with two loops check if the values for the same key (column name) differs, is this a totally wrong approach?
having an if (customerId1 != customerId2 || customerName1 != customerName2 || and so on for 50 fields is not very good.

Are you looking for a generic solution to this kind of thing? If so does your DataTable always contain 2 columns where the first can be treated as the dictionary key and the second the value? If so, then simply use the column index instead of the column name
var customerDictionary = new Dictionary<string, string>();
foreach (DataRow row in customerTbl.Rows)
customerDictionary.Add(row[0].ToString(), row[1].ToString());
If the values in the customerId and CompanyName columns are never null then instead of using ToString(), you can cast the values to a string
customerDictionary.Add((string)row[0], (string)row[1]);
FYI, the DataTable has a search capability as well (the Find() method). If you set up a primary key column the search will be extremely fast and you may not need to copy the data from a DataTable to a Dictionary.
Edit : A complete working example of one way of doing this in C# code as per the OPs edited question
class Program
{
static void Main(string[] args)
{
var table1 = GetCustomersFromFirst();
var table2 = GetCustomersFromSecond();
foreach (DataRow row in table1.Rows)
{
var foundRows = table2.Select("CustomerId = " + (string)row[0]);
if (foundRows.Length == 1)
{
var foundRow = foundRows[0];
foreach (DataColumn col in table1.Columns)
{
var valueOfColumnFromTable1 = row[col.Ordinal].ToString();
var valueOfColumnFromTable2 = foundRow[col.Ordinal].ToString();
if (String.Compare(valueOfColumnFromTable1, valueOfColumnFromTable2) != 0)
{
//the colum values are not the same.
}
}
}
else
{
// Something is wrong since more than one matching record was found
// or no matching records were found.
}
}
}
static DataTable GetCustomersFromFirst()
{
var dt = GetInitializedCustomerDataTable();
var row = dt.NewRow();
row[0] = "1";
row[1] = "CompanyA";
dt.Rows.Add(row);
row = dt.NewRow();
row[0] = "2";
row[1] = "CompanyB";
dt.Rows.Add(row);
row = dt.NewRow();
row[0] = "3";
row[1] = "CompanyC";
dt.Rows.Add(row);
return dt;
}
static DataTable GetCustomersFromSecond()
{
var dt = GetInitializedCustomerDataTable();
var row = dt.NewRow();
row[0] = "1";
row[1] = "CompanyA";
dt.Rows.Add(row);
row = dt.NewRow();
row[0] = "2";
row[1] = "CompanyD";
dt.Rows.Add(row);
row = dt.NewRow();
row[0] = "3";
row[1] = "CompanyC";
dt.Rows.Add(row);
return dt;
}
static DataTable GetInitializedCustomerDataTable()
{
var dt = new DataTable();
dt.Columns.Add("CustomerId", typeof(string));
dt.Columns.Add("CompanyName", typeof(string));
return dt;
}
}
In the test data, the 2nd row does not match. So it should fall into the condition where the comment says:
//the colum values are not the same.
Personally, I'd look at doing this in the database itself.

Try this.
Dictionary<string, string> customerDictionary = new Dictionary<string, string>();
foreach (DataRow row in customerTbl.Rows)
{
if(row["CustomerId"] != null && customerDictionay.ContainsKey("CustomerId") == false)
{
customerDictionary.Add(row["CustomerID"].ToString(), row["CompanyName"] as string);
}
}

Just a question, what's the benefit of putting the values in a dictionary? You are basically just eliminating the need of writing .ToString() when accessing the values...i.e, instead of:
row["CustomerID"].ToString();
you write
customerDictionary["CustomerID"];
Or have I missed something?

Related

How to display specific row of data based on a column value

From this code I am trying to only display data rows that contain the fields "DBY" in the Station_From_Code column and "MAT" in the Station_To_Column. This will then be printed onto a HTML page. These fields are definitely in the datatable but when I run this cod the table is empty. I am not used to working with data tables in C# so apologies for the poor coding.
dynamic schedluedContent = JsonConvert.DeserializeObject(scheduledJson);
JArray items2 = new JArray();
foreach (JObject stops in schedluedContent.stops)
{
DataTable dt = new DataTable();
DataRow dr;
dr = dt.NewRow();
dr["From_Station_Code"] = stops["station_code"];
dr["To_Station_Code"] = stops["station_code"];
dt.Rows.Add(dr);
DataRow[] result = dt.Select("From_Station_Code = 'DBY' AND To_Station_Code = 'MAT'");
dt.Rows.Add(result);
GridViewTrainTimes.DataSource = dt;
GridViewTrainTimes.DataBind();
}
It's a long time I have not seen this style of code for working with database tables! EntityFramework comes to change and surely ease the way of working with database and prevent different risks of using SQL commands inside the code.
If you use EF, your code will become something like bellow:
var rows = myDBContext.MyTable.Where(x=>x.From_Station_Code == "DBY" && x.To_Station_Code == "MAT");
GridViewTrainTimes.DataSource = rows;
You can use find match rows and add in new datatable, that contains only your match rows data then you can bind this dt to gridview.
DataTable dt = new Datatable(); // Lets this datatable have data;
Datatable dt2 = new Datatable(); // Copy all matching rows
foreach (DataRow dr in dt.Rows)
{
if (dr["From_Station_Code"].ToString() == "DBY" && dr["To_Station_Code"].ToString() == "MAT")
{
dt2.Rows.Add(dr);
}
}
GridViewTrainTimes.DataSource = dt;
GridViewTrainTimes.DataBind();

How to Edit a row in the datatable

I have created a data table. It has 3 column Product_id, Product_name and Product_price
Datatable table= new DataTable("Product");
table.Columns.Add("Product_id", typeof(int));
table.Columns.Add("Product_name", typeof(string));
table.Columns.Add("Product_price", typeof(string));
table.Rows.Add(1, "abc", "100");
table.Rows.Add(2, "xyz", "200");
Now I want to find by index, and update that row.
say for e.g.
I want to change value of Product_name column to "cde" that has the Product_id column value : 2.
First you need to find a row with id == 2 then change the name so:
foreach(DataRow dr in table.Rows) // search whole table
{
if(dr["Product_id"] == 2) // if id==2
{
dr["Product_name"] = "cde"; //change the name
//break; break or not depending on you
}
}
You could also try these solutions:
table.Rows[1]["Product_name"] = "cde" // not recommended as it selects 2nd row as I know that it has id 2
Or:
DataRow dr = table.Select("Product_id=2").FirstOrDefault(); // finds all rows with id==2 and selects first or null if haven't found any
if(dr != null)
{
dr["Product_name"] = "cde"; //changes the Product_name
}
You can find that row with
DataRow row = table.Select("Product_id=2").FirstOrDefault();
and update it
row["Product_name"] = "cde";
Try the SetField method:
By passing column object :
table.Rows[rowIndex].SetField(column, value);
By Passing column index :
table.Rows[rowIndex].SetField(0 /*column index*/, value);
By Passing column name as string :
table.Rows[rowIndex].SetField("product_name" /*columnName*/, value);
If your data set is too large first select required rows by Select(). it will stop further looping.
DataRow[] selected = table.Select("Product_id = 2")
Then loop through subset and update
foreach (DataRow row in selected)
{
row["Product_price"] = "<new price>";
}
You can traverse through the DataTable like below and set the value
foreach(DataTable thisTable in dataSet.Tables)
{
foreach(DataRow row in thisTable.Rows)
{
row["Product_name"] = "cde";
}
}
OR
thisTable.Rows[1]["Product_name"] = "cde";
Hope this helps
Try this I am also not 100 % sure
for( int i = 0 ;i< dt.Rows.Count; i++)
{
If(dt.Rows[i].Product_id == 2)
{
dt.Rows[i].Columns["Product_name"].ColumnName = "cde";
}
}
try this,
foreach(DataRow rw in dt.Rows)
{
rw["Obj_Amt"] = h.Decrypt(rw["Obj_Amt"].ToString());
dt.AcceptChanges();
rw.SetModified();
}
use SetModified() method to update data table value.

Looping through 2 datatable and adding values to third

I have created 3 datatables
var dt1= new DataTable();
var dt2= new DataTable();
var dt3= new DataTable();
Then i loop
foreach (DataRow row1 in dt1.Rows)
{
dt3.Rows.Add(row1.ItemArray);
foreach (DataRow row2 in dt2.Rows)
{
var Id2 = row1["Id"];
var Id1= row2["Id"];
if (Id1 == Id2)
{
dt3.rows["Name"] = "" ; // doesnt work
}
}
}
As you can see that i loop on 2 datatables. Then in inner loop i check if the records matches. Now if the record matches then i want to update column "Name" on dt3 datatable.
I tried using
dt3.rows["Name"] = "" ;
But this doesnt work. I know the reason that its because i again need to loop on dt3 datatable and
assign values to column in that loop. But not sure how to do it and if there is even more better solution. I mean we can find id in dt3 datatable and then update value. But not sure how to do it
Is there more intelligent solution than looping on 2 table?
The first foreach is not needed. A simple DataTable.Copy will bring all the data and structure from the original table in the destination table. Then looping on the second table and Select on the third to find the matching rows and clear the name.
dt3 = dt1.Copy();
foreach (DataRow row2 in dt2.Rows)
{
DataRow[] match = dt3.Select("ID=" + row2["ID"].ToString());
if(match.Lenght > 0)
match[0]["Name"] = "" ;
}
Not sure if this is more performant from the other answers. Need to be tested
dt3.rows doesn't work, you want to change the name of the row that you have added now. This should work:
foreach (DataRow row1 in dt1.Rows)
{
DataRow newRow = dt3.Rows.Add(row1.ItemArray);
foreach (DataRow row2 in dt2.Rows)
{
var Id2 = row1["Id"];
var Id1 = row2["Id"];
if (Id1 == Id2)
{
newRow["Name"] = "new Name";
}
}
}
Try this:
foreach (DataRow row1 in dt1.Rows)
{
var row = dt3.Rows.Add(row1.ItemArray);
foreach (DataRow row2 in dt2.Rows)
{
var Id2 = row1["Id"];
var Id1= row2["Id"];
if (Id1 == Id2)
{
row["Name"] = ""; //maybe works
}
}
}
How about,
dt2Lookup = new HashSet(
dt2.AsEnumerable().Select(row => row.Field<int>("Id")));
dt3 = dt1.Clone();
forreach (var row In dt1.AsEnumerable())
{
var newRow = dt3.Rows.Add(row.ItemArray)
if (dt2lookup.Contains(row.Field<int>("Id"))
{
newRow.SetField("Name", string.Empty);
}
}
The HashSet should provide good lookup performance.
dt3 = dt1.Copy();
var RowDictionary = dt3.Rows.OfType<DataRow>().ToDictionary(dr => dr["ID"].ToString());
//replace by Dictionary<string,List<DataRow>> in case ID is not unique and fill it with a foreach loop.
foreach (DataRow row2 in dt2.Rows)
{
DataRow Match;
if (c.TryGetValue(row2["ID"].ToString(), out Match))
{
Match["Name"] = "";
}
}
You should call:
dt3.rows[dt3.rows.Count - 1].Columns["Name"] = "" ;

How to expand abbreviations in datatable row by row c#

I'm using datatable that has two columns of text data, I just want to do abbreviations expanding for each row, so I used Dictionary and here it is a sample of my code:
private void ExpandWords()
{
DataTable DT = new DataTable();
DataRow Row;
DT.Columns.Add(new System.Data.DataColumn("Name", typeof(string)));
DT.Columns.Add(new System.Data.DataColumn("Label", typeof(string)));
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("emp", "employee");
dict.Add("dep", "department");
for (int i = 0; i < dataGridView6.Rows.Count; i++)
{
Row = DT.NewRow();
foreach(KeyValuePair<string,string> rep in dict)
{
Row[0] = dataGridView6.Rows[i].Cells[0].Value.ToString().Replace (rep.Key, rep.Value);
Row[1] = dataGridView6.Rows[i].Cells[1].Value.ToString().Replace (rep.Key, rep.Value);
}
DT.Rows.Add(Row);
dataGridView7.DataSource = DT;
}
it can be run without exceptions but it doesn't work
Why do you enumerate the dictionary in the loop of the grid-rows? Just lookup the key.
You also have to add each row in the loop, not outside, otherwise you just add the last row:
for (int i = 0; i < dataGridView6.Rows.Count; i++)
{
Row = DT.NewRow();
string abbrCell1 = dataGridView6.Rows[i].Cells[0].Value.ToString();
string abbrCell2 = dataGridView6.Rows[i].Cells[1].Value.ToString();
Row[0] = dict[abbrCell1];
Row[1] = dict[abbrCell2];
DT.Rows.Add(Row);
}
Edit If you want to replace all abbreviated words in the string of the datagrid cells with the not-abbreviated word in the dictionary you can use this code:
// in the loop
Row = DT.NewRow();
string abbrCell1 = dataGridView6.Rows[i].Cells[0].Value.ToString();
string abbrCell2 = dataGridView6.Rows[i].Cells[1].Value.ToString();
IEnumerable<string> unabbrWords1 = abbrCell1.Split()
.Select(w => dict.ContainsKey(w) ? dict[w] : w);
IEnumerable<string> unabbrWords2 = abbrCell2.Split()
.Select(w => dict.ContainsKey(w) ? dict[w] : w);
Row[0] = string.Join(" ", unabbrWords1);
Row[1] = string.Join(" ", unabbrWords2);
DT.Rows.Add(Row);
You should change it a bit otherwise you'll have same value overwritten in loop:
var value1 = dataGridView6.Rows[i].Cells[0].Value.ToString();
var value2 = dataGridView6.Rows[i].Cells[1].Value.ToString();
foreach (KeyValuePair<string, string> rep in dict)
{
value1 = value1.Replace(rep.Key, rep.Value);
value2 = value2.Replace(rep.Key, rep.Value);
}
DT.Rows.Add(value1, value2);
You're also missing DataBind() if you're using Web Forms at the end
dataGridView7.DataSource = DT;
dataGridView7.DataBind();
mz

How to bind DataRow to the GridView?

I have one datatable which has four or five columns. I dont know exactly the columns name and its count. But I want to bind the first row of the datatable into the GridView. How to do this? I need all your suggestions please.
Linq should be helpful here to get first item.
var Temp = dt.AsEnumerable().Take(1).CopyToDataTable();
use the filter in the datatable :
dt.Select("ID = 1");
you can try like this..
dt = new DataTable();
dt_Property.Columns.Add("Field1");
int i = 0;
DataRow row = null;
foreach (DataRow r in ds.Tables[0].Rows)
{
row = dt.NewRow();
row["Field1"] = ds.Tables[0].Rows[i][1];
dt_Property.Rows.Add(row);
i = i + 1;
}
dataGridView1.DataSource = dt;

Categories

Resources