adding a column to a specific row in dataset - c#

I have a dataset.
I want to iterate it and add a column (currently not in my dataset) with different value to every row. More specificly, i want it to be at 0 index.
I want to check the value in "fullname" column, and then add a new column with Id.
What do i need to write?
I iterate like this:
foreach (DataRow theRow in mesakem.Tables["fullname"].Rows)
foreach(oved o in ovdimlist)
if(o.name==theRow.ToString())
add column(o.id)......
Ty very much!

I think you meant to insert a new column at index 0 and then add id values to cell for each row where current name cell value matches your object name. If I'm right, it should look like this:
DataColumn col = mesakem.Tables["fullname"].Columns.Add("Id");
col.SetOrdinal(0);
foreach (DataRow row in mesakem.Tables["fullname"].Rows)
{
foreach (oved o in ovdimlist)
{
if (o.name == row["Name"].ToString())
row["Id"] = o.id;
}
}

Related

I want to check if datagridview column is having data or null/empty

I want to check if my datagridview value stored in column[2] is empty or null.
How can I access datagridview column?
string[] row = new string[]
{
Convert.ToString(dataReader[0]),
Convert.ToString(dataReader[1]),
Convert.ToString(dataReader[2]),
Convert.ToString(dataReader[3])
};
dataGridView1.Rows.Add(row);
whit this code you can access column from your DataGridView
foreach (DataGridViewColumn col in dataGridView1.Columns)
but for checking null values you should check the cells not the column , so just add another for to count your rows and check every cells of your specific column
your code will be like this:
for (int colCounter=0 ; colCounter<dataGridView1.Columns.Count; colCounter++)
for(int rowCounter=0 ; rowCounter<dataGridView1.Rows.Count ; rowCounter++)
if(DataGridView1.Rows[rowCounter].Cells[colCounter].Value.tostring()==null)
//your code
I think you need to use a loop to add those data in your datagridview like this:
foreach(var data in row)
{
dataGridView1.Rows.Add(data);
}

DataTable: swich 2 Columns without header

My Goal is to switch 2 Columns like this:
datatable.Columns["column1"].SetOrdinal(1);
datatable.Columns["column2"].SetOrdinal(0);
Now what i'm trying to achieve, is that the column header(column1, column2) stay at their place not like the other values.
You want to change the row-field order:
foreach(DataRow row in datatable.Rows)
{
object oldCol2 = row["column2"];
row["column2"] = row["column1"];
row["column1"] = oldCol2;
}
But note that both columns should have the same type.

Add columns into a DataTable inside a loop

I'd like to know if there is a way to automatically add columns in a DataTable inside a foreach loop? That is what I mean with "automatically".
I had in mind something like this:
DataTable dt = new DataTable();
foreach (var item in model.Statistik)
{
dt.Columns.Add();
row = dt.NewRow();
row[//Name of column goes here] = // either item or item.property;
dt.Rows.Add(row);
}
This is much more efficient than explicitly name every column before the for each loop. Because, what happens if some property changes or gets deleted, etc.? This is therefore, something I wish to get rid of.
I don't think you understand how dt.Columns and dt.Rows are related. There is one set of Columns for a DataTable. Every Row in the DataTable has all the matching columns - you don't have unique Columns in each Row. So every time you do dt.Columns.Add you are adding a column to every Row, old or new.
Also, how do you expect Row[<name of column>] to work if you don't specify the name to the DataTable?

GetChanges from merged datatables returns null

I have following code:
DataTable datTable3 = new DataTable();
datTable3 = datTable1.Clone();
datTable2.Merge(datTable1);
datTable3 = datTable2.GetChanges();
Want I want to do is: Compare DataTable1 with DataTable2 and when there are rows in DataTable1 which aren't in DataTable 2 then add these rows into a new DataTable(3). This code above gives me an empty DataTable3 each time although the rows in the first dt are not equal to the rows in my second dt. what am I doing wrong? Sorry if that question may be too easy but I'm using C# since a couple of months.
EDIT: I found this solution which doesn't work for me... Why?
DataTable datTable3 = new DataTable();
datTable3 = datTable1.Clone();
foreach (DataRow row in datTable1.Rows)
{
datTable3.ImportRow(row);
}
foreach (DataRow row in datTable3.Rows)
{
row.SetAdded();
}
datTable2.Merge(datTable3);
DataTable datTableFinal = datTable2.GetChanges(DataRowState.Added);
// shows me a datatable with again the values from datTable1
// even if they are already in datTable2!
datTable2.RejectChanges();
datTable1.RejectChanges();
The DataTable.GetChanges() method Gets a copy of the DataTable that contains all changes made to it since it was loaded or AcceptChanges was last called.
In other words, GetChanges() is dependent on the DataRow.RowState property. A DataTable.Merge() will either preserve their 'RowState' property, or reset it to 'Unchanged'.
This means that when you merge two DataTables with rows that have 'Unchanged' RowStates, the merged table will also contain 'Unchanged' rows and the DataTable.GetChanges method will return null or Nothing.
EDIT : You can always iterate through the DataTable to see what rows are added to the merged table. Something like
foreach(DataRow row in datTable2.Rows)
{
Console.WriteLine("--- Row ---"); // Print separator.
foreach (var item in row.ItemArray) // Loop over the items.
{
Console.Write("Item: "); // Print label.
Console.WriteLine(item); // Invokes ToString abstract method.
}
}
Iterate through and use LoadDataRow(object[] value, bool fAcceptChanges) :
foreach (DataRow row in MergeTable.Rows)
{
TargetTable.LoadDataRow(row.ItemArray, false);
}
var changes = TargetTable.GetChanges();
changes had the desired value when I tried this method.

get index of DataTable column with name

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 without doing this?
You can use DataColumn.Ordinal to get the index of the column in the DataTable. So if you need the next column as mentioned use Column.Ordinal + 1:
row[row.Table.Columns["ColumnName"].Ordinal + 1] = someOtherValue;
Warning:
This code returns the next column, so the one after ColumnName, as requested in the question.
Try this:
int index = row.Table.Columns["ColumnName"].Ordinal;
You can simply use DataColumnCollection.IndexOf
So that you can get the index of the required column by name then use it with your row:
row[dt.Columns.IndexOf("ColumnName")] = columnValue;
I wrote an extension method of DataRow which gets me the object via the column name.
public static object Column(this DataRow source, string columnName)
{
var c = source.Table.Columns[columnName];
if (c != null)
{
return source.ItemArray[c.Ordinal];
}
throw new ObjectNotFoundException(string.Format("The column '{0}' was not found in this table", columnName));
}
And its called like this:
DataTable data = LoadDataTable();
foreach (DataRow row in data.Rows)
{
var obj = row.Column("YourColumnName");
Console.WriteLine(obj);
}

Categories

Resources