Add same row and column to datable - c#

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);

Related

How to assign one datarow values to another datarow

I want to copy first row of one DataTable to first row of another DataTable. I did as shown below but it throws error like
Property or indexer 'System.Data.DataRowCollection.this[int]' cannot be assigned to -- it is read only
DataTable dt = GetTable();
DataTable dt1 = GetTable1();
dt.Rows[0] = dt1.Rows[0];
You have to copy it field by field.
The data row within the datatable is immutable, which means that you can change the values of its fields, but not the object itself, being said that you should copy the values from you source row to the destination row.
Assuming your datatables have the same columns:
DataTable dt = GetTable();
DataTable dt1 = GetTable1();
foreach(DataColumn column in dt.Columns)
{
dt.Rows[0][column] = dt1.Rows[0][column];
}
Hope this helps.

Add DataTable Row to DataGridView without binding

I have a predefined DataGridView that I need to add rows to from a DataTable without data binding. I am trying to use the DataGridView.Rows.Add() method programmatically however, I do not know the column names of the DataTable. The columns in the DataTable are in the same order as the DataGridView but how do I add them to the DataGridView without knowing the column names?
Say your DataGridView exists but has no columns. You can do this:
foreach (DataColumn dc in yourDataTable.Columns) {
yourDataGridView.Columns.Add(new DataGridViewTextBoxColumn());
}
Then add the row data:
foreach(DataRow dr in yourDataTable.Rows) {
yourDataGridView.Rows.Add(dr.ItemArray);
}
Now, if the default textbox column isn't sufficient, you might have to create a column with a different cell template.
it seems that you want to get the column names from the DataTable and add the rows to DataGridView from DataTable rows
DataTable myDataTable = new DataTable();
//adding Columns
myDataTable.Columns.Add("colInt", typeof(int));
myDataTable.Columns.Add("colDate", typeof(DateTime));
myDataTable.Columns.Add("colString", typeof(string));
//adding Rows
myDataTable.Rows.Add(1, DateTime.Now, "Hello World");
//to get columns
foreach (DataColumn col in myDataTable.Columns)
{
var c = new DataGridViewTextBoxColumn() { HeaderText = col.ColumnName }; //Let say that the default column template of DataGridView is DataGridViewTextBoxColumn
dataGridView1.Columns.Add(c);
}
//to get rows
foreach (DataRow row in myDataTable.Rows)
{
dataGridView1.Rows.Add(row[0], row[1], row[2]);
}
anyway there's a shortcut
dataGridView1.DataSource = myDataTable;
if your DataGridView haven’t rows and columns., then just
yourDataGridView.DataSource = yourDataTable
will do all job.
if your DataGridView is already bounded to some datasource(if you using DataTable then I presume DataSource is DataTable),
then you need to edit yourDataTable -> add old rows from old DataTable(or from DataGridView if old DataTable not accessible anymore)
foreach(DataRow dr in oldDataTable.Rows)
{
yourDataTable.Rows.Add(dr);
}
yourDataGridView.DataSource = yourDataTable;
or edit oldDataTable -> add new rows from yourDataTable, something like:
DataTable dtOld = (DataTable)yourDataGridView.DataSource;
foreach(DataRow yourdr in yourDataTable.Rows)
{
dtOld.Rows.Add(yourdr);
}
yourDataGridView.DataSource = dtOld;

Adding data to a row in a Dataset?

I am trying to add data to a row in a dataset but the data is always on a new row?
I need the data to populate under its column. I need something like Ds.Tables[0].Rows[1].add("Item")
This is how i am inserting the data:
DataSet ds = new DataSet();
ds.Tables.Add("Properties");
//GPS
ds.Tables[0].Columns.Add(ArrayProperties[0].FormMobiField);
ds.Tables[0].Rows.Add(ArrayProperties[0].Value);
//Street Num and Name
ds.Tables[0].Columns.Add(ArrayProperties[3].FormMobiField);
ds.Tables[0].Rows.Add(ArrayProperties[3].Value);
//Suburb
ds.Tables[0].Columns.Add(ArrayProperties[6].FormMobiField);
ds.Tables[0].Rows.Add(ArrayProperties[6].Value);
//City
ds.Tables[0].Columns.Add(ArrayProperties[7].FormMobiField);
ds.Tables[0].Rows.Add(ArrayProperties[7].Value);
//Province
ds.Tables[0].Columns.Add(ArrayProperties[8].FormMobiField);
ds.Tables[0].Rows.Add(ArrayProperties[8].Value);
//Locality Map
ds.Tables[0].Columns.Add(ArrayProperties[9].FormMobiField);
ds.Tables[0].Rows.Add(ArrayProperties[9].Value);
//Property Type
ds.Tables[0].Columns.Add(ArrayProperties[10].FormMobiField);
ds.Tables[0].Rows.Add(ArrayProperties[10].Value);
Just get a new Row from the DataTable and then add that row to the table, Use DataTable.NewRow method
DataRow dr = ds.Tables[0].NewRow();
dr["Column1"] = "value";
dr["Column2"] = "value";
dr["Column3"] = "value";
ds.Tables[0].Rows.Add(dr);
You are adding row after adding each column, You may first create your data table's structure by adding all the columns and then you can get the new row using DataTable.NewRow() and later you can add that row to your data table. After adding all the columns you may also try:
ds.Tables[0].Rows.Add(ArrayProperties[0].Value,ArrayProperties[1].Value,ArrayProperties[2].Value,ArrayProperties[3].Value);
The columns collection of a Datatable regards the table structure. In your code you mix adding columns and populating fileds.
You should first create the structure (not tested and syntax errors can occur):
Dataset ds = new Dataset();
Datatable dt = new Datatable();
dt.columns.add(new Column.add(...));
...
dt.columns.add(new Column.add(...));
ds.Tables.add(dt);
And then:
Datarow r = ds.tables[0].NewRow();
r["column1"] = value1;
...
r["columnX"] = valueX;
ds.Tables[0].rows.add(r);
See this msdn article for more details.
Add the columns as you are adding. For populating rows, do the below.
foreach (DataRow row in ds.Tables[0]) // Loop over the rows.
{
row[ArrayProperties[i].FormMobiField]=ArrayProperties[0].Value;
i++;
}
If it doesn't work then let me know,

dynamic table in c# for database rows

I have a table with a column called totalnames.This table has 10 rows.
Now i want to dynamically create table for this 10 rows with check box in my web page.Please send some good examples.
Sample Code : Dynamically create table, add cloumn, add rows
(1) create a new DataTable 
DataTable dt = new DataTable ("Table_AX"); 
(2) Add columns to the DataTable 
// Method 1 
dt.Columns.Add ("column0", System.Type.GetType ("System.String")); 
// Method 2 
DataColumn dc = new DataColumn ("column1", System.Type.GetType ("System.Boolean")); 
dt.Columns.Add (dc); 
(3) to add rows to the DataTable 
// Initialize the row 
DataRow dr = dt.NewRow (); 
dr ["column0"] = "AX"; 
dr ["column1"] = true; 
dt.Rows.Add (dr); 
// Doesn't initialize the row 
DataRow dr1 = dt.NewRow (); 
dt.Rows.Add (dr1); 
If you want to Copy DataTable include data try
 
DataTable dtNew = dt.Copy (); 

How to filter a specific value from a data table

I have five rows in my data table (with column AccountId, Name, Email, Address) and I want to get a specific row on basis of AccountId as all the five rows have different AccountID. I want to filter it on the basis of AccountID. I mean I only need one row from the Data table to process on the basis of AccountId.
How do I get a specfic row from the data table containing the AccountId that I have passed?
Three options:
Use DataTable.Select, providing a filter expression
Iterate over the rows yourself
Use LINQ, with the data table extensions
Personally I'd suggest using the last option (LINQ):
var row = table.AsEnumerable()
.FirstOrDefault(r => r.Field<string>("AccountID") == accountID);
if (row != null)
{
// Use the row
}
Have you looked into the DataTable.Select() method?
http://msdn.microsoft.com/en-us/library/system.data.datatable.select(v=vs.100).aspx
public class DataTableExample
{
public static void Main()
{
//adding up a new datatable
DataTable dtEmployee = new DataTable("Employee");
//adding up 3 columns to datatable
dtEmployee.Columns.Add("ID", typeof(int));
dtEmployee.Columns.Add("Name", typeof(string));
dtEmployee.Columns.Add("Salary", typeof(double));
//adding up rows to the datatable
dtEmployee.Rows.Add(52, "Human1", 21000);
dtEmployee.Rows.Add(63, "Human2", 22000);
dtEmployee.Rows.Add(72, "Human3", 23000);
dtEmployee.Rows.Add(110,"Human4", 24000);
// sorting the datatable basedon salary in descending order
DataRow[] rows= dtEmployee.Select(string.Empty,"Salary desc");
//foreach datatable
foreach (DataRow row in rows)
{
Console.WriteLine(row["ID"].ToString() + ":" + row["Name"].ToString() + ":" + row["Salary"].ToString());
}
Console.ReadLine();
}
}
Example with an array:
http://msdn.microsoft.com/en-us/library/f6dh4x2h(VS.80).aspx
Example with a single Object:
http://msdn.microsoft.com/en-us/library/ydd48eyk
Just use something like this:
DataTable dt = new DataTable();
DataRow dr = dt.Rows.Find(accntID);
Hope this helped you out.

Categories

Resources