I have a DataTable and I access its rows using a foreach loop
foreach (DataRow row in table.Rows)
{
userId = Convert.ToInt32(row[BundleSchema.ParamUpdatedBy]);
}
but knowing that my DataTable contains only a single row, how can I access it without looping through it?
You can use Index on Rows
DataRow row = table.Rows[0];
userId = Convert.ToInt32(row[BundleSchema.ParamUpdatedBy]);
Related
DataTable table = new DataTable();
foreach (DataColumn column in dt.Columns)
{
table.Columns.Add(column.ColumnName, typeof(string));
}
I have one datable dt which have data I am creating one new Datatable and adding a column into that Datatable then my table looks like shown in below image
Now show in image column name as a number, fullname, address, date, and so on I also want to add that same column as data row but I don't know how can I add it is like data row.
And every time column name changes so when I am adding it on the Datatable table as column name I also need to add it is a data row.
Please help me to do that
I found it myself using the below code
List<string> names = new List<string>();
DataTable table = new DataTable();
DataRow firstRow = table.NewRow();
foreach (DataColumn column in dt.Columns)
{
names.Add(column.ColumnName);
table.Columns.Add(column.ColumnName, typeof(string));
}
firstRow.ItemArray = names.ToArray();
table.Rows.InsertAt(firstRow, 0);
And it works as I want and thanks for the reply.
You can try this type of logic:- First Add row after that in that rows add a column
DataTable table = new DataTable();
DataRow row = table.NewRow();
table.Rows.Add(number, fullname, address, address);
Is it possible to get a row value by giving column name when DataTable holds a single row, without iteration.
foreach(DataRow row in dt.Rows)
{
string strCity = row["City"].ToString();
}
Table
I need Something like below without loop when we have only one row,
String cn=row["ColumnName"].ToString()
This is the way:
string Text = dataTable.Rows[0]["ColumnName"].ToString();
Use following code to get the value of the first row of the DataTable without iteration:
string strCity = string.Empty;
if (yourDataTable.Rows.Count > 0)
strCity = yourDataTable.Rows[0]["City"].ToString();
Convert.ToString(row["ColumnName"]);
This is attempting to index the row itself:
row["ColumnName"].ToString()
What you're looking for is to index the items within the row:
row.Item["ColumnName"].ToString()
when DataTable holds a single row, without iteration
If you're guaranteed that there is a row, you can reference it directly:
dt.Rows[0].Item["ColumnName"].ToString()
As with any indexing, you should probably do some bounds checking before trying to index it though.
DataTable dt = new DataTable();
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
StringBuilder html = new StringBuilder();
foreach (DataRow row in dt.Rows)
{
html.Append(row.Field<Int32>("Columname"));
}
}
I have a datagridview, and i want to make a datatable from it with some conditions. For example, when user double click one row, i want to make that row a datatable, or datagridview has a checkboxcell, i want a datatable from that grid which check column is checked. How can i do that with linq? Is there any linq way of this ?
I get a datatable with sqldataadaptor from database , and bind it to datagridview with
dataGridView.DataSource = myDataTable;
i have a working code for this, when user doubleclicks one cell in datagridview, i get that row to datatable like this
foreach (DataGridViewColumn column in dataGridView.Columns)
dataTable.Columns.Add(column.Name, column.ValueType);
dataTable.Rows.Add();
for (int j = 0; j < dataGridView.Columns.Count; j++)
{
dataTable.Rows[0][j] = dataGridView.Rows[e.RowIndex].Cells[j].Value;
}
I just wonder that, if there is a linq way to have this result a datatable ,like
dataTable =dataGridView.Rows.Cast<DataGridViewRow>().Where(f=>f.Index==e.RowIndex) //rowindex is the double clicked rows index here
This code will get you started:
DataTable dt = new DataTable();
foreach (DataGridViewColumn column in dataGridView1.Columns)
dt.Columns.Add(column.Name);
DataGridViewSelectedRowCollection row = this.dataGridView1.SelectedRows;
foreach (DataGridViewRow dr in row)
dt.Rows.Add(dr);
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;
}
}
I have two data tables, dt1 & dt2. dt1 has data while dt2 is a new table. Now I want to copy some rows, satisfying particular condition, to dt2. I have tried following code, but I get this exception:
System.ArgumentException This row already belongs to another table.
foreach(DataRow dr in dt1.Rows)
{
if(Convert.ToInt32(dr["col"]) == value)
{
dt2.Rows.Add(dr);
}
}
How can I resolve this?
change dt2.Rows.Add(dr); to dt2.Rows.Add(dr.ItemArray);
You directly cannot "Add" a DataTable row from one to another since it belongs to souce Datatable. You can "Add" a newly created row to DataTable.
Hence you are getting this exception.
There are many ways to overcome this. Some are listed below
Method 1:
If you want to get a row from source Datatable and add in destination without any changes, then use ImportRow this way.
dt2.ImportRow(dr);
Method 2:
But if you want to take specific data. Then use this
foreach(DataRow dr in dt1.Rows)
{
if(Convert.ToInt32(dr["col"]) == value)
{
Datarow ndr = dt2.NewRow();
ndr["Foo"] = dr["Foo"];
//..Similarly for other rows.
dt2.Rows.Add(ndr);
}
}
Every time when you add a row to the datatable it should be different(new) row........
Datarow newRow = null;
foreach(DataRow dr in dt1.Rows)
{
if(Convert.ToInt32(dr["col"]) == value)
{
newRow = dt2.NewRow();
newRow ["A"] = dr["A"];
dt2.Rows.Add(newRow );
}
}