Copy Data Table to a Data Grid without binding them together - c#

I have two datagrids in my application (dataGridView1 and dataGridView2). I am moving selected items from dataGridView1 into dataGridView2. Here is how I am currently doing this:
DataTable dt = new DataTable("dt");
DataTable id = new DataTable("id");
try
{
if (dataGridView1.Rows.Count > 0)
{
for (int i = 0; i < dataGridView1.Rows.Count - 0; i++)
{
if (dataGridView1.Rows[i].Cells[0].Value != null)
{
DataRow row;
DataRow idRow;
row = dt.NewRow();
idRow = id.NewRow();
idRow["id"] = dataGridView1.Rows[i].Cells[1].Value.ToString();
row["id"] = dataGridView1.Rows[i].Cells[1].Value.ToString();
row["Link Name"] = dataGridView1.Rows[i].Cells[2].Value.ToString();
dt.Rows.Add(row);
id.Rows.Add(idRow);
}
}
dataGridView2.DataSource = dt;
}
However, I also need to be able to remove items from dataGridView2. Currently, the DataTable dt is bound to dataGridView2 and I cant clear it after the items are added because it also clears the dataGridView2.
Basicly, my question would be, is there a way to add the contents of a data table to a data grid without using datagrid.DataSource?

You can manually add each row using the dataGridView2.Rows.Add function. Also, I have typically used the dataGridView2.Row(n).Tag to hold the actual source of that row's data.
int n = dataGridView2.Rows.Add();
DataGridViewRow newRow = dataGridView2.Rows[n];
newRow.Cells[0].Value = "ABC";
newRow.Cells[1].Value = 123;
newRow.Tag = row;

Related

sending datatable value to another form and create a loop and shown it in dgv

i have one form in which I create a datable globally. in datatable data is save dynamically. when every I press add button to save the data , a popup screen shown. at that screen i have a grid view in which i want to loop over some data and show it in that table....
all i need to know that how can i get the value from datable and create a loop and show data in data grid view
register form = new register();
DataTable dt = new DataTable();
dt=form.purchasedtable;
int frooms=int.Parse(dt.Columns["Froms"].ToString());
string to = dt.Columns["To"].ToString();
for (int i =frooms; i <= int.Parse(to); i++)
{
DataTable dtt = new DataTable();
dataGridView2.Rows.Add(dtt);
}
this is what i did but this is not working
Assuming you are using Winforms
var dataTable = new DataTable();
// Get All Rows
var rows = dataTable.Rows;
// Loop Over all rows
foreach (DataRow dataRow in rows)
{
//Get All Columns in Rows
var rowArray = dataRow.ItemArray;
foreach (var col in rowArray)
{
if (col is int)
{
var number = (int) col;
Console.WriteLine(number.ToString());
}
if (col is string)
{
var number = (string) col;
Console.WriteLine(number.ToString());
}
if (col is double)
{
var number = (double) col;
Console.WriteLine(number.ToString());
}
}

C# GridView rows added in wrong place?

I want to add rows to my GridView while the function is running.
Before function for adding rows is called there is always 1 "DATA" row present from previous function.
To add rows I tried to use:
int n = grid.Rows.Count;
grid.Rows.Add("Step" + n.ToString());
and I would expect to get rows with labels:
Data...
Step1...
Step2...
Step3...
etc
instead I get:
Step1...
Step2...
Step3...
etc
Data...
(DATA row is pushed to the very bottom, all rows are added above it)
Where is my mistake?
EDIT:
I tried:
int n = grid.Rows.Count;
DataGridViewRow row = new DataGridViewRow();
grid.Rows.Insert(n, row);
grid.Rows[n].Cells[0].Value = "Step"+n.ToString();
but it bugs out.
You can create DataTable and then show it on GridView!
try this:
System.Data.DataTable table = new DataTable();
table.Columns.Add("col1", typeof(string));
table.Columns.Add("col2", typeof(string));
table.Columns.Add("col3", typeof(string));
...
this is creating the rows:
for (int i = 0; i < n - 1; i++)
{
DataRow dr = table.NewRow();
if(i==0)
{
dr[col1]="Data"
dr[col2]= ...
dr[col3]= ...
...
}
if(i>0)
{
// your code
}
table.Rows.Add(dr);
}
to add this table to GridView:
DataView dv1 = new DataView(table);
dataGridView1.DataSource = dv1;
Use Insert to add the records and Count for the last position
Something like:
grid.Rows.Insert(grid.Rows.Count-1, row);

row.clone is not working when the datatable bind to datasourse

datagridview dgv is bind to datatable dt.
dgv_copy is new datagridview.dgv_copy is add row to dgv by using row.clone.
my code is
if (dgv_copy.Columns.Count == 0)
{
foreach (DataGridViewColumn dgvc in dgv_org.Columns)
{
dgv_copy.Columns.Add(dgvc.Clone() as DataGridViewColumn);
}
}
DataGridViewRow row = new DataGridViewRow();
for (int i = 0; i < dgv_org.Rows.Count; i++)
{
row = (DataGridViewRow)dgv_org.Rows[i].Clone();
int intColIndex = 0;
foreach (DataGridViewCell cell in dgv_org.Rows[i].Cells)
{
row.Cells[intColIndex].Value = cell.Value;
intColIndex++;
}
dgv_copy.Rows.Add(row);
}
dgv_copy.AllowUserToAddRows = false;
dgv_copy.Refresh();
source is https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewrow.clone.aspx
Because original DataGridView is bounded to the DataTable you can create copy of DataTable and boud it to the second DataGridView.
var original = (DataTable)originalDataGridView.DataSource;
var copy = original.Copy();
copyDataGridView.DataSource = copy;
Pretty sure you could just do dgv_copy.rows.add(dgv_org.Rows[i].ItemArray)

datatable get specific column from row

I have the following:
//a datatable with some rows and columns lets say 5x5
var datatable = new DataTable();
var numberofrows = datatable.rows.count;
for (int i = 0; i < numberofrows; i++) {
//for each row, get the 3rd column
var cell = datatable.rows[i].???
}
how do I get the 3rd column for each row?
for 3rd column
var cellValue = datatable.Rows[i][2];
better, if you know the column name,
var cellValue = datatable.Rows[i]["column_name"];
If the table contains 2 columns "Property" and "Value". Contents can be inserted as follows
table.Columns.Add("Property", typeof(string));
table.Columns.Add("Value", typeof(string));
table.Rows.Add("P1", "abc");
table.Rows.Add("P2", "xyz");
To retrive contents of specific column
foreach (DataRow row in table.Rows)
{
if (row["Property"].ToString() == "P1")
{
var value = row["Value"].ToString();
}
}
we can read the particular column values by like this
foreach (DataTable table in ds.Tables)
{
if (table.TableName == "Table1")
{
for (int j = 0; j < table.Rows.Count; j++)
{
int a = Convert.ToInt32( table.Rows[j].ItemArray[3]);
int b = Convert.ToInt32(table.Rows[j].ItemArray[4]);
}
}
}
You can iterate thru the datatable and get every row, which can be thought as an array, so you can pick each element of that particular row using an index (and can also use the name of the column instead of the index, i.e.: row["column3 name"]).
foreach(DataRow row in datatable)
{
Console.WriteLine(row[2]);
}

How find datarows and change index of datarow in an existing datatable?

I have an existing DataTable with thousand rows and want to find the rows and set it to become top rows.
I don't know if you're able to, but I would probably add a dummy column to sort by, give the rows you want a higher value in the dummy column, and then sort by the column from bigger to smaller with a DataView.
private void ModifyTable(DataTable toModify)
{ //Add a column to sort by later.
DataColumn col = toModify.Columns.Add("DummySort", typeof(int));
col.DefaultValue = 0;
}
private void SetDummyColumnValue(DataRow dr, int value)
{ //Mark the columns you want to sort to bring to the top.
//Give values bigger than 0!
dr["DummySort"] = value;
}
private DataTable GetSortedTable(DataTable modifiedTable)
{
//Sort by the column from bigger to smaller
DataView dv = new DataView(modifiedTable);
dv.Sort = "DummySort DESC"; return dv.ToTable();
}
You can try to remove and insert row
public void MoveDataRowTo(DataRow dataRow,int destination)
{
DataTable parentTable = dataRow.Table;
int rowIndex = parentTable.Rows.IndexOf(dataRow);
if (rowIndex > 0)
{
DataRow newDataRow = parentTable.NewRow();
for (int index = 0; index < dataRow.ItemArray.Length; index++)
newDataRow[index] = dataRow[index];
parentTable.Rows.Remove(dataRow);
parentTable.Rows.InsertAt(newDataRow, destination);
dataRow = newDataRow;
}
}
DataSet dsAct = new DataSet();
OleDbDataAdapter odaTem = new OleDbDataAdapter("SELECT ActivityId, Activity FROM tbl_Activity", ocnConn);
odaTem.Fill(dsAct, "Activity");
DataRow drTem;
int i = 0;
//Rows want set to be on top is in the table "TopActivity"
foreach (DataRow dr in dsAct.Tables["TopActivity"].Rows)
{
drTem = dsAct.Tables["Activity"].NewRow();
//Clone the row
drTem.ItemArray = dsAct.Tables["Activity"].Rows.Find(dr["Activity_ID"]).ItemArray;
dsAct.Tables["Activity"].Rows.RemoveAt( dsAct.Tables["Activity"].Rows.IndexOf( dsAct.Tables["Activity"].Rows.Find(dr["Activity_ID"])));
dsAct.Tables["Activity"].Rows.InsertAt(drTem, i);
i++;
}

Categories