can anyone do help..?
i really need help on this, here i have two forms [form1 & form2]. I have created datagridview(DGV) in each forms. Now i need to transfer/copy
You can simply declare the dataset/datatable
which binds your form1
grid
as a public and access it in form2
. Via that datatable or dataset
you can again bind grid on form2.
Or you can send dataset or datatable from form1 to form2(possibly through modular function) and then bind the grid.
This is the simplest way.
You should use some kind of event
for this purpose:
//the Form1 class
public partial class Form1 : Form {
Form2 f2 = new Form2();
public Form1(){
InitializeComponent();
f2.TransferSelectedRows += (s,e) => {
if (dataGridView1.RowCount > 0) {
foreach(DataGridViewRow row in dataGridView1.SelectedRows){
//add the selected row to the receiver grid
e.ReceiverGrid.Rows.Add(row.Cells.OfType<DataGridViewCell>()
.Select(cell=>cell.Value).ToArray());
//remove the selected row
dataGridView1.Rows.Remove(row);
}
e.HasRows = true;
} else {
e.HasRows = false;
}
};
}
}
//Form2 class
public partial class Form2 : Form {
public Form2() {
InitializeComponent();
}
public class TransferSelectedRowsEventArgs : EventArgs {
public DataGridView ReceiverGrid {get; private set;}
public bool HasRows {get;set;}
public TransferSelectedRowsEventArgs(DataGridView receiver){
ReceiverGrid = receiver;
}
}
public delegate void TransferSelectedRowsEventHandler(object sender, TransferSelectedRowsEventArgs e);
public event TransferSelectedRowsEventHandler TransferSelectedRows;
protected virtual void OnTransferSelectedRows(TransferSelectedRowsEventArgs e){
TransferSelectedRowsEventHandler handler = TransferSelectedRows;
if(handler != null) handler(this, e);
}
//Click event handler for button1
private void button1_Click(object sender, EventArgs e){
var transfer = new TransferSelectedRowsEventArgs(dataGridView1);
OnTransferSelectedRows(transfer);
if(!transfer.HasRows){
MessageBox.Show("There is no data to export, please verify..!", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
NOTE: The code I posted above is not complete, some members or code of your own should be added. You should also consider using DataSource
for your Grid, that's is how we deal with DataGridView
in real projects.
Declare Form1's DGV as Public instead of private and do the following in Form2 :
Declare the following variable :
System.Windows.Forms.Form f = System.Windows.Forms.Application.OpenForms["Form1"];
.
Do the Following changes in your program:
private void button1_Click (object sender, EventArgs e)
{
try
{
if (dataGridView1.RowCount != 0)
{
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
((Form1)f).dataGridView1.Rows.Add(row);
}
}
else
{
MessageBox.Show("There is no data to export, please verify..!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch { } }
Declare a public BindingList<row_object>
in Form 2 and write it from Form 1. If the datagridview in Form 1 is bound to the bindingList, the the data will automatically refresh. See this to view how to bind a datagridview to a bindingList.