Transfering selected rows from data grid view to another data grid view - c#

My problem is to transfer selected rows from data grid view to another data grid view. I don't know what to use if I use the primary key and get the values from database to the another database or send the selected row's data from datagridview1 to datagridview2

You can create a new DataTable and insert all the selected rows to that DataTable
DataTable GetSelectedRows(DataGridView dgv)
{
var dt = new DataTable();
foreach (DataGridViewColumn column in dgv.Columns)
{
if (column.Visible)
{
// You could potentially name the column based on the DGV column name (beware of dupes)
// or assign a type based on the data type of the data bound to this DGV column.
dt.Columns.Add();
}
}
object[] cellValues = new object[dgv.Columns.Count];
foreach (DataGridViewRow row in dgv.Rows)
{
if (!row.Selected) continue; // Add only Selected Rows
for (int i = 0; i < row.Cells.Count; i++)
cellValues[i] = row.Cells[i].Value;
dt.Rows.Add(cellValues);
}
return dt;
}
You can pass all SelectedRows to new DataGridView using this
dataGridView2.DataSource = GetSelectedRows(dataGridView1)

Related

How to programmatically add rows to DataGrid in C#?

So as the title states, I'm trying to add rows to a DataGrid programmatically using C# but I can't seem to make it work. This is what I have so far.
// I have a DataGrid declared as dg in the XAML
foreach (string s in array) {
int index = 0;
DataGridRow dgRow = new DataGridRow();
foreach (DataGridColumn dgColumn in columns)
{
// Trying to add Text to dgRow here but IDK how
index++;
}
}
I've been googling around and the closest I got to adding anything was to use {column = value} but it just throws me an error. Really out of ideas now though :\
Here's you can do it better way by binding a source to datagridview
// Creating DataSource here as datatable having two columns
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name");
// Adding the rows in datatable
for (int iCount = 1; iCount < 6; iCount++)
{
var row = dt.NewRow();
row["ID"] = iCount;
row["Name"] = "Name " + iCount;
dt.Rows.AddRow(row);
}
DataGridView dgv = new DataGridView();
// Set AutoGenerateColumns true to generate columns as per datasource.
dgv.AutoGenerateColumns = true;
// Finally bind the datasource to datagridview.
dgv.DataSource = dt;
In Case you are using WPF DataGrid
you can bind it like this way-
dgv.DataContext = employeeData.DefaultView;
and in
XAML
<DataGrid Name="dgv" ItemsSource="{Binding}">
//create datatable and columns,
DataTable dtable = new DataTable();
dtable.Columns.Add(new DataColumn("Column 1"));
dtable.Columns.Add(new DataColumn("Column 2"));
//simple way create object for rowvalues here i have given only 2 add as per your requirement
object[] RowValues = { "", "" };
//assign values into row object
RowValues[0] = "your value 1";
RowValues[1] = "your value 2";
//create new data row
DataRow dRow;
dRow = dtable.Rows.Add(RowValues);
dtable.AcceptChanges();
//now bind datatable to gridview...
gridview.datasource=dbtable;
gridview.databind();
Regards
did you try?:
int n=5; // number of rows you want to add
dataGridView1.Rows.Add(n);
// you can add (names of the rows) if you have them in your array
//for(int i=0; i<n; i++)
//dataGridView1[0, i].Value = array[i];

Set value for cell item based on column header and row in DataTable

I have a DataTable binding to a WPF datagrid where I've created columns and created a set number of rows, with the first column populated with default values in each row. It looks like this table:
So I have the Size column and all its values and the headers for each column. Now I need to input the data I collected into my DataTable but not using the indexes for columns and rows, I need to find the cell item using the header titles for row and column. Here's what I have:
heatMapTbl.Rows['2000000']['BOA'] = statVal;
Any suggestions?
Hint:
DataTable table = new DataTable();
var myColumn = table.Columns.Cast<DataColumn>().SingleOrDefault(col => col.ColumnName == "myColumnName");
if (myColumn != null)
{
// just some roww
var tableRow = table.AsEnumerable().First();
var myData = tableRow.Field<string>(myColumn);
// or if above does not work
myData = tableRow.Field<string>(table.Columns.IndexOf(myColumn));
}
I looped through the rows like this:
foreach (DataRow row in LiqTbl.Rows)
{
if (row.ItemArray[0].Equals(timezone))
{
foreach (DataColumn column in LiqTbl.Columns)
{
if (column.ColumnName.Equals(lp))
{
//Write the value in the corresponding row
row[column] = liq.ToString("N", CultureInfo.InvariantCulture);
}
}
}
}

how to add selected rows of datagridview to a datatable

I started working with Windows Form Application recently. In a datagridview DatagridView1 I am displaying some data from a datatable dt1.
DatagridView1.DataSource=dt1;
I have added a column in DatagridView1 named selectRows of type DataGridViewCheckBoxColumn.
I want to add the selected rows of the DatagridView1 to another datatable.
I have a button btn_Select, On button click I am getting the selected rows in a string format using following code:
private void btn_Select_Click(object sender, EventArgs e)
{
string data = string.Empty;
foreach (DataGridViewRow row in DatagridView1.Rows)
{
if (row.Cells[0].Value != null &&
Convert.ToBoolean(row.Cells[0].Value) == true)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.OwningColumn.Index != 0)
{
data += (cell.Value + " "); // do some thing
}
}
data += "\n";
}
}
}
But I want to select the whole row, not just text.
Use the DataBoundItem property to get the data in the entire row. With DataTable.ImportRow method you can import the selected rows data into a different table.
Sample Code:
DataRow datarow = null;
DataTable dt = new DataTable();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if(Convert.ToBoolean(row.Cells[CheckBoxColumn1.Name].Value) == true)
{
datarow = ((DataRowView)row.DataBoundItem).Row;
dt.ImportRow(datarow);
}
}
I think you are looking for this. This is exactly what you want.
DataGridView checkbox column - value and functionality

Bind DataTable to Datagridview that already have column defined

I am trying to Bind DateTable to Datagridview that already have columns designed using Designer in VS.
Source for DataTable is sql database.
I am trying to do this using following code which adds only blank rows in datagridview.
dataGridView1.AutoGenerateColumns = false; // Disable autogeneration of columns
DataTable dt = new DataTable();
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
dt.Columns.Add(col.Name);
}
dt = get_data_table("select * from Mytable");
dataGridView1.DataSource = dt;
DataGridView columns have property named DataPropertyName just set them to your DataTable column names and you are set.
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
dt.Columns.Add(col.Name);
col.DataPropertyName = col.Name;
}
this should work.

Cannot add new Row to Datatable

Merged with Add new Row to DataTable.
I am trying to add two new Rows to a DataTable. My DataSource consist of a Database with one Table (FooTable) and one column (FooName).
See following code. Only the first Row "Mary" is added and displayed on my DataGrid, but not the second row "Jesus". No exception occurs. Why doesn't it work and what is the correct way to add multiple rows dynamically to DataTable?
/* dg1 = DataGrid; ds = DataSet */
dg1.ItemsSource = ds.Tables;
/* dt = DataTable */
DataRow newRow = dt.NewRow();
newRow["FooName"] = "Mary";
dt.Rows.Add(newRow);
/* add another row to data table */
DataRow newRow2 = dt.NewRow();
newRow2["FooName"] = "Jesus";
dt.Rows.Add(newRow2);

Categories

Resources