I am using two grid view in the different form, I want to pass the form1 all cells values
another Form[form2]. form2 data view in one cell. then click the form2 cell load the all data view in form1 cells.
this form1 ->
private void button1_Click(object sender, EventArgs e)
{
string[] colB = new string[dataGridView1.Rows.Count];
for (int i = 0; i < dataGridView1.Rows.Count -1; i++)
{
colB[i] = Convert.ToString(dataGridView1.Rows[i].Cells[0].Value);
}
}
how to get data form2->
?
Forms are classes like any other. However, they have window-specific behaviors they inherit from the Form class.
This means you can specify your own constructor and pass along any information you would like. For instance, let's say we have a Person class we want to show in a new Form. We could define a new PersonForm to show the details (this is just an example. You will be able to adapt it to your needs).
public class PersonForm : Form
{
PersonForm(Person model)
{
InitializeComponents(); //I think this is the name of the method.
//Do whatever is needed here with the model.
}
}
So when a user double clicks the right cell, you create a new instance of the PersonForm and you show it.
Related
In the form1 i create a datagridview that fill rows with database.
in the form2 i want to show the user the specific row of datagridview from form1.
specific row is chosen by user !!!
i set my datagridview in public modifier and that is my form 2 code:
form1 parentsell = new form1();
string selecteduser = parentsell.propertydatagrid.Rows[selectedrowindex].Cells[72].Value.ToString();
MessageBox.Show(selecteduser);
thats no error when program is build but after run that give me an Err.
So, you probably using lookup Form, you need to pass value by reference, and easiest way is to crate class User like this:
public Class User
{
public string name {get; set;}
}
Then you can make instance of that object in main Form, like this
User user = new User();
form1 parentsell = new form1(user);
parentsell.ShowDialog(); // This will open another form and wait you to finish work
MessageBox.Show(user.name);
In form1 you need to create another constructor and instance of User
User user;
public form1(User user)
{
InitializeComponent();
this.user = user;
}
Now you need some button that will say "Ok, I found this user, now take him and close this form" like this:
private void button1_Click(object Sender, EventArgs e)
{
user.name = ...; //find your user, DGV.selectedRows[0].Cells[somehing].Value.ToString(); I think it's like this...
this.Close();
}
And you should get your selected user in Main form
A very simple approach:
Add this line to the other form(e.g. Form2) where you want to access the datagridview:
public static DataGridView view { get; set; }
Then go to the form(e.g. Form1) that contains the DataGridView(e.g. dataGridView1) that you want to access.
Add this line to that form:
foreach(DataGridViewRow row in dataGridView1.Rows)
{
Form2.view.Rows.Add(row.Cells["Column1"].Value);
Form2.view.Rows.Add(row.Cells["Column2"].Value);
}
Add a textbox(e.g. textBox1) for the user to add the index of the required row and get it in a new datagridview(e.g. dataGridView2) in this way:
dataGridView2.Rows.Clear();
dataGridView2.Rows.Add(view.Rows[Convert.ToInt32(textBox1.Text)].Cells["Column1"].Value, view.Rows[Convert.ToInt32(textBox1.Text)].Cells["Column2"].Value)
Remember that a datagridview row index starts from '0'.
Also remember to add two columns to the dataGridView2 before running the code.
You can also directly call data from the database to the required form instead of fetching it from another datagridview.
I have a form (Form1) which contains a grid, bound to a DataSource via a BindingSource.
I then have a button, which once clicked opens another form (Form2) that should let a user enter new data.
I pass the BindingSource from Form1 to Form2, and the goal is that once the user "saves" his input in Form2, it'll be automatically added to Form1.
Is there a way to do this, w/o directly accessing the UI controls?
I.E -
public partial class Form2 : Form
{
BindingSource bs = new BindingSource();
public Form2(BindingSource bindingSourceFromForm1)
{
InitializeComponent();
this.bs = bindingSourceFromForm1;
}
private void button1_Click(object sender, EventArgs e)
{
DataRow dr = (this.bs.DataSource as DataTable).NewRow();
dr["Col1"] = this.textBox1.Text;
dr["Col2"] = this.textBox2.Text;
this.bs.Add(dr);
}
}
Is there a way to bind the controls on Form2 (in the example above textBox1/2) to the BindingSource, and then have it automatically add the values in textBox1 & 2?
Something like calling this.bs.Add(), where Add() knows where to take it's values without me explicitly telling it to go to the textboxes, since it's bound to the aforementioned controls?
Thanks!
If you add a BindingSource to the form designer as you would normally do, set the DataSource to the same source so you can bind the textboxes.
In the specialized constructor the following code adds a new record to the DataSource of form1, assigns the DataSource to the DataSource of the BindingeSource instance of this form and sets the position. Your new Form will enable the user the enter the values in that new object.
public Form2(BindingSource bindingSourceFromForm1)
: this()
{
bindingSourceFromForm1.AddNew();
this.bindingSource1.DataSource = bindingSourceFromForm1.DataSource;
this.bindingSource1.Position = bindingSourceFromForm1.Position;
}
If your user can cancel the operation you have to compensate for that by calling RemoveCurrent on the bindingSourceFromForm1 but I leave that as excersice as it is not clear if you want/need that.
I've been working on my datagridview properties and wondering how can I pass the value of my selected data row to another form.
private void dgvRptView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
var senderGrid = (DataGridView)sender;
if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn &&
e.RowIndex >= 0)
{
Form Update = new frmUpdateSvcRep();
Update.Show();
}
}
apparently, I was only able to add button inside the datagridview and added an event when I clicked the button, it'll show a form. However. I've been trying to pass the values I selected to textbox in another from but to no avail. Can someone please help me figure out how to pass the value where I click my button? here's my image caption.
here's my another form looks like when I click the edit button inside the Datagridview.
I'm really out of my depth for now.. I'm opt on creating constructors but I don't know how to implement it in this Scenario. Thanks in Advance
There are several ways to accomplish passing data between Forms. One way, as you mentioned, is to pass via the constructor when you instantiate the Form:
private void dgvRptView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dgvRptView.Columns[e.ColumnIndex] is DataGridViewButtonColumn &&
e.RowIndex >= 0)
if (dgvRptView.CurrentRow != null)
{
var row = dgvRptView.CurrentRow.Cells;
DateTime age = Convert.ToDateTime(row["MyColumn"].Value);
string name = Convert.ToString(row["MyName"].Value);
Form Update = new frmUpdateSvcRep(age, name);
Update.Show();
}
}
Update the constructor of your other Form to accept those parameters:
public class Update : Form
{
public Update(DateTime age, string name)
{
// do whatever you want with the parameters
}
...
}
It might be tempting to just pass the entire dgvRptView.CurrentRow object, but I'd advise against that. Your other Form then has to know about the columns in the DataGridView so it can access the values, which is not something it should be concerned about and could lead to runtime bugs when the column names change.
I have a datalist in form1. How I can bring the selected datalist items to form2?
That is code in form1. When I click to context menu form2 comes. I want to bind selected items's common datas in a table to a datagrid in form2.
protected void showKontextMenu(string key, int col = -1, WlistRow row = null)
{
int cou = datalist.SelectedItems.Count;
if (utilDB.isAllowed(utilDB.eDBRights.eEditor) && cou>1)
{
cm.Items.Add(new MenuItem() { Header = utilText.sstr("Stichworte und Attribute"), Name = "AttrEdit", Tag = key });
}
cm.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(KontextMenu_Click));
cm.IsOpen = true;
}
As mentioned by Dejo also, there are many ways. But I guess the simplest and most effective way is following.
Create static variables in the main program.
Assign values to those variables from from1.
Access them from form2.
Hope you get it (without the sample code!)
save selected value in some variable in form1 & access that variable from form2
There are many ways.
Common are to save data to a for example object, and access it from another class (form)
You can also make constructor with type datalist as a parameter and send your data with constructor call.
You could expose a Property on your Form1 and access it from Form2
Add a property on your Form1
private DataList _formOneList
public DataList FormOneList
{
get{return _formOneList;}
}
Access it on your Form2
private DataList FormTwoList
Form2.FormTwoList=Form1.FormOneList;
i have a datagridview that show my data in columns.
what i'm trying to accomplish is that after choosing a row and pressing edit button a new form will open and split the row for the right text boxes to update the data.
the datagridview row shows different types of data: name,email,date,etc...
any idea?
Thanks in advance!
This site explains how to send data between forms, it would be as simple as selecting the right cell in the datagrid, sending that info off to the right textbox, for all of them. then sending them back. Data between forms
The basics are to create a method that can be use to get the value,
public string getTextBoxValue()
{
return TextBox.Text;
}
then you can just call the method to pass the data between the forms,
this.Text = myForm2.getTextBoxValue();
however you will be sending the values of the cells, and will be making a textbox.text equal to the return of the method
this is a basic example of the theory, giev it a try yourself to make it work for what you want it to do, if you just cant do it come back and ask for help and ill edit with the code, but only after youve tried yourself first
You can create a class, say MyDataCollection, with properties corresponding to your DataGridView columns. When you press the Edit button, create a new instance of this class, fill it with the necessary data and pass it as parameter to the EditForm's constructor.
public class MyDataCollection
{
public string Name;
public string Email;
// --
}
In your main form:
void btnEdit_Click(object sender, EventArgs e)
{
// Create the MyDataCollection instance and fill it with data from the DataGridView
MyDataCollection myData = new MyDataCollection();
myData.Name = myDataGridView.CurrentRow.Cells["Name"].Value.ToString();
myData.Email = myDataGridView.CurrentRow.Cells["Email"].Value.ToString();
// --
// Send the MyDataCollection instance to the EditForm
formEdit = new formEdit(myData);
formEdit.ShowDialog(this);
}
And the edit form should look like this:
public partial class formEdit : Form
{
// Define a MyDataCollection object to work with in **this** form
MyDataCollection myData;
public formEdit(MyDataCollection mdc)
{
InitializeComponent();
// Get the MyDataCollection instance sent as parameter
myData = mdc;
}
private void formEdit_Load(object sender, EventArgs e)
{
// and use it to show the data
textbox1.Text = myData.Name;
textbox2.Text = myData.Email;
// --
}
}
You can also forget about the MyDataCollection class and pass the entire DataGridViewRow to the formEdit's constructor.