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 ();
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);
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.
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;
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,
I have a DataGrid with 5 template columns,
However when I try and add some dynamically created controls into the grid, it fails, as there are no rows.
-Can i add a blank row in and use that? and how?
-Or any other way?
I'm pretty sure you have to bind to a data source. But it's easy enough to create your own DataTable and insert a row into it with some dummy info.
//pseudo code:
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("column1");
dt.Columns.Add(dc);
DataRow dr = dt.NewRow();
dr["column1"] = "value1";
dt.Rows.AddNew(dr);
myDataGrid.DataSource = dt;
myDataGrid.DataBind();
If you are using an unbound DataGridView, you can create new rows and then add them to DataGridView. Your question referred to DataGrid, but you tagged it for DataGridView.
// Sample code to add a new row to an unbound DataGridView
DataGridViewRow YourNewRow = new DataGridViewRow();
YourNewRow.CreateCells(YourDataGridView);
YourNewRow.Cells[0].Value = "Some value";
YourNewRow.Cells[1].Value = "Another value";
YourDataGridView.Rows.Add(YourNewRow);