Adding data to a gridview in c# from multiple forms - c#

How would I go about inserting the text that I have entered in to the textbox in NewActivity into the first column in the datagridview on form1?
Here is the coding I have thus far.
Form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.IsMdiContainer = true;
}
private void viewToolStripMenuItem1_Click(object sender, EventArgs e)
{
}
private void newActivityToolStripMenuItem_Click(object sender, EventArgs e)
{
NewActivity NewAc = new NewActivity();
NewAc.MdiParent = this;
NewAc.Show();
}
private void deleteActivityToolStripMenuItem_Click(object sender, EventArgs e)
{
}
}
}
NewActivity
public partial class NewActivity : Form
{
public string activityName;
public NewActivity()
{
InitializeComponent();
}
private void btnCancel_Click(object sender, EventArgs e)
{
activityName = "";
this.Close();
}
private void btnAddActivity_Click(object sender, EventArgs e)
{
activityName = txtActivityName.Text;
this.Close();
}
}
}

you can insert it in your event click
private void btnAddActivity_Click(object sender, EventArgs e)
{
activityName = txtActivityName.Text;
int index = yourDataGridView.Rows.Add();
DataGridViewRow row = yourDataGridView.Rows[index];
row.Cells[0].Value = activityName ;
this.Close();
}

Here is an example of how to bind data from a textbox control to a DataGrid
// create new row
DataGridViewRow row = new DataGridViewRow();
// create cells
row.CreateCells(this.dataGridView1, textBox1.Text, textBox2.Text, textBox3.Text);
// add to data grid view
this.dataGridView1.Rows.Add(row);
---------------Below is how you would use it in your case--------------------
private void btnAddActivity_Click(object sender, EventArgs e)
{
activityName = txtActivityName.Text;
int index = dgvActivityList .Rows.Add();
DataGridViewRow row = dgvActivityList .Rows[index];
row.Cells[0].Value = activityName;
this.Close();
}

Related

Xamarin - Morse code app usuing the flaslight

I am trying to make a morse code for a college project, what I'm trying to do is use a 2 dimensional array to save the morse code people input to a text file and then be able to load it from the text file, my logic was to was that within the array was this array[morse name][morse input]. what I need to figure out first is how to send data from methods / buttons OBtn_Clicked , LBtn_Clicked, SBtn_Clicked and EndBtn_Clicked to NewMorseBtn_Clicked to add into the array which will then write it out to a text file I've created.
namespace FlashLightApp2018
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MorsePage : ContentPage
{
//bool exitLoop = false;
public MorsePage()
{
InitializeComponent();
}
private async void NewMorseBtn_Clicked(object sender, EventArgs e)
{
bool isTextEmpty = String.IsNullOrEmpty(MorseName.Text);
if (isTextEmpty)
{
}
else
{
OBtn.IsEnabled = true;
LBtn.IsEnabled = true;
SBtn.IsEnabled = true;
EndBtn.IsEnabled = true;
// String morseName = MorseName.Text;
//String[,] morseSave = new String[100,100];
}
//File.WriteAllText(morseName, text);
//while (exitLoop != true)
//{
//}
}
private void LoadMorseBtn_Clicked(object sender, EventArgs e)
{
}
private void PlayMorseBtn_Clicked(object sender, EventArgs e)
{
}
private void OBtn_Clicked(object sender, EventArgs e)
{
}
private void LBtn_Clicked(object sender, EventArgs e)
{
}
private void SBtn_Clicked(object sender, EventArgs e)
{
}
private void EndBtn_Clicked(object sender, EventArgs e)
{
}
}
}
first, declare you data at the class level (outside of a single method) so that it is accessible from throughout your class
string morseData = string.Empty;
then have your different button methods update the data
private void OBtn_Clicked(object sender, EventArgs e)
{
morseData += ".";
}
private void LBtn_Clicked(object sender, EventArgs e)
{
moreseData += "-";
}

How to disable a combobox on clicking on another combox list data in C sharp?

I want to design a windows form using C sharp on Visual Studio 2013.
I go through the Source from here. but did not got it properly.
for that I have 3 combobox. I want to disable combobox2 when I click on combobox1 NSSCM element and enable when click on NSSFO element.
Below is my part of code snippet:
namespace NSE_First_Form
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MaximizeBox = false;
MinimizeBox = false;
if (true)
{
comboBox1.Items.Add(Exchange.NSSCM.ToString());
comboBox1.Items.Add(Exchange.NSSFO.ToString());
comboBox1.Items.Add(Exchange.BSSCM.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
string selectedItem = string.Empty;
ProcessValue(selectedItem);
}
public enum Exchange
{
NSSCM = 1,
NSSFO = 2,
BSSCM = 3
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
Try this:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == 0)
comboBox2.Enabled = false;
if (comboBox1.selectedIndex == 1)
comboBox2.Enabled = true;
}
Try this:
//This will disable combobox2 on the click of it
private void comboBox1_Click(object sender, EventArgs e)
{
comboBox2.Enabled = false;
}
//This will enable combobox2 on the click of it
private void comboBox1_Click(object sender, EventArgs e)
{
comboBox2.Enabled = true;
}
Because you want it on click, use the CLICK event, instead of SelectedIndexChange event.

Add items from a listbox to another listbox on another form

Here is my code.
private void PlaceOrder_Click(object sender, EventArgs e)
{
MenuBox.Items.Clear();
TotalBox.Items.Clear();
total.Clear();
ordertotal = 0;
}
I want to add what is in the menu box to a another list box on another form.
Update
(added by jp2code)
Form1 (Main):
namespace WindowsFormsApplication1 {
public partial class RESTAURANT : Form
{
double soup = 2.49;
double ordertotal;
public RESTAURANT()
{
InitializeComponent();
}
private void RESTAURANT_Load(object sender, EventArgs e)
{
}
private void Add_Click(object sender, EventArgs e)
{
MenuBox.Items.Add("Soup");
TotalBox.Items.Add(String.Format("{0:C}", soup));
ordertotal += soup;
total.Text = Convert.ToString(String.Format("{0:C}", ordertotal));
}
private void TotalBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void PlaceOrder_Click(object sender, EventArgs e)
{
new AreYouSure().Show();
this.Show();
MenuBox.Items.Clear();
TotalBox.Items.Clear();
total.Clear();
ordertotal = 0;
}
}
}
Form2 (Confirmation)
namespace WindowsFormsApplication1 {
public partial class Confirmation : Form
{
public Confirmation()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Confirmation_Load(object sender, EventArgs e)
{
}
private void MenuBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
When clicking the 'Send Order' button the items from 'MenuBox' in form 1 need to be sent to the 'MenuBox' in form 2
OtherForm.OtherListbox.Items.Clear();
foreach(var itm in MenuBox.Items)
OtherForm.OtherListbox.Items.Add(itm);
Form1 (Main):
namespace WindowsFormsApplication1 {
public partial class RESTAURANT : Form
{
double soup = 2.49;
double ordertotal;
public RESTAURANT()
{
InitializeComponent();
}
private void RESTAURANT_Load(object sender, EventArgs e)
{
}
private void Add_Click(object sender, EventArgs e)
{
MenuBox.Items.Add("Soup");
TotalBox.Items.Add(String.Format("{0:C}", soup));
ordertotal += soup;
total.Text = Convert.ToString(String.Format("{0:C}", ordertotal));
}
private void TotalBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void PlaceOrder_Click(object sender, EventArgs e)
{
new AreYouSure().Show();
this.Show();
MenuBox.Items.Clear();
TotalBox.Items.Clear();
total.Clear();
ordertotal = 0;
}
}
}
Form2 (Confirmation)
namespace WindowsFormsApplication1 {
public partial class Confirmation : Form
{
public Confirmation()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Confirmation_Load(object sender, EventArgs e)
{
}
private void MenuBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
When clicking the 'Send Order' button the items from 'MenuBox' in form 1 need to be sent to the 'MenuBox' in form 2
It is better that the controls on your other form (ListBox, in this case) are set to Private by default.
In that case, you would either need to set the control's visibility to Public (bad form, in my opinion) or create a method in your other form to accept the parameters from your form.
Consider something like this:
public void ListBoxData(object[] array)
{
listBox1.Clear();
listBox1.AddRange(array);
}
To get the data or selected item information back to your main form, you would likewise create another public object that you could check, like the property below:
public object SelectedItem { get { return listBox1.SelectedItem; } }
I hope that is what you were looking for.
UPDATE:
Using the code you supplied in the post below, I can see you do not have anything in your Confirmation form to send data to, much less a way to pass that information.
If you had a ComboBox, you could do something like follows:
public partial class Confirmation : Form
{
private ComboBox comboBox1;
public void AddRange(object[] array)
{
comboBox1.Items.AddRange(array);
}
}
That does not place the ComboBox anywhere on your form. You would need to work that out.
With that done, I'm guessing you need to edit your PlaceOrder_Click routine:
private void PlaceOrder_Click(object sender, EventArgs e)
{
//new AreYouSure().Show();
//this.Show();
using (var obj = new Confirmation())
{
var list = new List<object>(MenuBox.Items.Count);
foreach (var o in MenuBox.Items)
{
list.Add(o);
}
obj.AddRange(list.ToArray());
if (obj.ShowDialog(this) == DialogResult.OK)
{
MenuBox.Items.Clear();
TotalBox.Items.Clear();
total.Items.Clear();
ordertotal = 0;
}
}
}
If you are struggling with this, you might need to look into some C# Windows "multiple forms" tutorials.
Here is a YouTube (that I did not sit all the way through): https://www.youtube.com/watch?v=qVVtCPDu9ZU

How to get value from one form to an other?

I made a form for deleting/removing products.
Now my question is how do I get the value from one form to the other so I can use this to update the amount of products or delete the products from a database?
I'm trying to get the value of tbAantal.Text, so I can use this in my other form to update the amount of products.
Or should I do it in a other way?
public partial class Bevestiging : Form
{
public Bevestiging()
{
InitializeComponent();
Aantal = 0;
}
public int Aantal { get; set; }
private void btnOk_Click(object sender, EventArgs e)
{
int aantal;
if (!int.TryParse(tbAantal.Text, out aantal))
{
MessageBox.Show("U kunt alleen numerieke waardes invullen.", "Fout");
return;
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void BtUp_Click(object sender, EventArgs e)
{
Aantal++;
tbAantal.Text = Aantal.ToString();
}
private void BtDown_Click(object sender, EventArgs e)
{
Aantal--;
tbAantal.Text = Aantal.ToString();
}
So I can use this to update it here:
private void gridGeregistreerd_ColumnButtonClick(object sender, ColumnActionEventArgs e)
{
var dialog = new Bevestiging();
if (DialogResult.OK != dialog.ShowDialog()) ;
}
You've already made the public property "Aantal" on your first form with the right get/set so to retrieve the value on your second form use this:
using (Bevestiging myForm = new Bevestiging())
{
DialogResult result = myForm.ShowDialog();
if (result != DialogResult.OK)
{
int returnedValue = myForm.Aantal;
}
}
In you form, you have already define a public property :
public partial class Bevestiging : Form
{
public int Aantal {
get; set;
}
}
Then in your callee form, you can access it :
private void gridGeregistreerd_ColumnButtonClick(object sender, ColumnActionEventArgs e)
{
var dialog = new Bevestiging();
if (DialogResult.OK != dialog.ShowDialog())
{
int aantal = dialog.Aantal;
/* Save it to database or whatever you want */
}
}

PetaPoco update just modified records in winforms

I have this code :
namespace PetaPocoTest
{
public partial class Form1 : Form
{
PetaPoco.Database db = new PetaPoco.Database("PgConnection");
IEnumerable<customers> allCustomers;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
allCustomers = db.Query<customers>("SELECT * FROM customers");
mGrid.DataSource = allCustomers .ToList();
}
private void saveButton_Click(object sender, EventArgs e)
{
foreach (var a in allCustomers)
{
db.Save("customers", "custumer_id", a);
}
}
}
}
bat this updates all record, no matter if they are changed or not.
So, my question is does anyone know how to update only changed records in petapoco ?
This is how I did it (eventually!) :
namespace PetaPocoTest
{
public partial class Form1 : Form
{
PetaPoco.Database db = new PetaPoco.Database("PgConnection");
Dictionary<string, int> modRows = new Dictionary<string, int>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
bindingSource = db.Fetch<customers>("SELECT * FROM customers");
mGrid.DataSource = bindingSource;
}
private void saveButton_Click(object sender, EventArgs e)
{
db.BeginTransaction();
foreach (customers c in bindingSource)
{
if (modRows.ContainsKey(c.id.ToString()))
{
db.Save("customers", "id", c);
}
}
db.CompleteTransaction();
}
private void mGrid_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
int recId = ((customers)bindingSource.Current).id;
if (!modRows.ContainsKey(recId.ToString()))
{
modRows.Add(recId.ToString(), recId);
}
}
}
}
It functions, but if anyone has better idea pls share!
This is the simplest way from me to do this by db.BeginTransaction on Form_Load
private void Form1_Load(object sender, EventArgs e)
{
studentBindingSource.DataSource = db.Query<student>("SELECT * FROM student");
db.BeginTransaction(); // Begin Transaction here
}
On Update (CellValueChanged) just simply do db.Save() this doesn't commit yet because our transaction is not complete yet
private void studentDataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
db.Save((student)studentBindingSource.Current);
}
And yes we can do Insert and Delete as well !!!
Insert
private void buttonInsert_Click(object sender, EventArgs e)
{
student newStudent = new student
{
StudentName = "POCONEW"
}
studentBindingSource.Add(newStudent);
db.Save(newStudent);
}
Delete
private void studentDataGridView_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
int rowIndex = e.Row.Index; // We use Gridview row index instead because BindingSource.Current isn't work when user drag then deleting multiple row
db.Delete(studentDataGridView.Rows[rowIndex].DataBoundItem as student);
}
Finally to save change we just do db.CompleteTransaction()
:Just a little note here after user press the Save Button if you don't close the form you have to call db.BeginTransaction() again if not everything user edit after this will be saved automatically because we don't have Transaction anymore
private void btSave_Click(object sender, EventArgs e)
{
db.CompleteTransaction();
db.BeginTransaction(); // everything user edit after this will be saved automatically because we don't have Transaction anymore so we Begin it again
}

Categories

Resources