add data to gridView on form from another class - c#

How can i append data to my dataGridView on form, from another class?
here is class:
class TermSh
{
public HttpWebRequest request_get_page_with_captcha;
public HttpWebResponse response_get_page_with_captcha;
public string url;
public Form1 form1;
public BindingSource bindingSource1 = new BindingSource();
public int id = 0;
public TermSh(Form1 form1)
{
this.form1 = form1;
form1.dataGridView1.DataSource = bindingSource1;
}
public void somemethod()
{
try
{
cookies += response_get_page_with_captcha.Headers["Set-Cookie"];
bindingSource1.Add(new Log(id++, DateTime.Now, cookies));
form1.dataGridView1.Update();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
and form class:
TermSh term_sh = new TermSh(this);
term_sh.somemethod();
what i do wrong? why my datagridview is empty after code executing, but with debug i see, that bindingSource1 is not empty.
how to add data?

I think , the way you are going to achieve your goal is incorrect.
first of all, I think passing Form class to a class is very very bad. and then you can simply manipulate a list and return the value and using this value (list) in your Form.
I think it's better to do like this:
[EDIT 1] this following class, is your ptimary class that has a method and this method return a new Log, and you can add this return value to the datagridview in the Form1.
class TermSh
{
public HttpWebRequest request_get_page_with_captcha;
public HttpWebResponse response_get_page_with_captcha;
public string url;
public int id = 0;
public List<Log> somemethod()
{
try
{
cookies += response_get_page_with_captcha.Headers["Set-Cookie"];
return new Log(id++, DateTime.Now, cookies); //use this return value in your Form and update datagridview
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
}
[EDIT 2] after that: you must prepare Log Class to be used as a collection in bindingSource (Form1.bindingSource) and update gridView. and the Following code show the Log class:
class Log
{
private int id;
private DateTime datetime;
private string log_text;
public Log(int id, DateTime datetime, string log_text)
{
this.id = id;
this.datetime = datetime;
this.log_text = log_text;
}
#region properties
public int ID { get { return id; } set { id = value; } }
public DateTime DATE_TIME { get { return datetime; } set { datetime = value; } }
public string LOG_TEXT { get { return log_text; } set { log_text = value; } }
#endregion
}
[Edit 3] and this code in the Form1, use the return value of class TermSh, and populate the dataGridView:
TermSh term_sh = new TermSh(city, type, null, null);
logList.Add(term_sh.getPageWithCaptchaConnection());
logBindingSource.DataSource = logList;
logBindingSource.ResetBindings(false);
[EDIT 4] so if you have a question that : "how use this class as a collection in bindingSource??". It's simple, you can populate a dataGridView with objects: this article is helpful.

Related

How do I populate a grid with a list of list of object? in c# [duplicate]

I have a List that contains a series of transaction objects. What I'm trying to do is to display these transaction objects in a Datagridview control on loading a form, basically the Datagridview should represent something of a transaction register to display the data for each of the transaction objects in the list.
I must admit to a lack of experience when it comes to using Datagridviews and I'm having some difficulty with understanding what I need to do here.
My question is, how do I go about getting the details of each of the objects in the list to display in the Datagridview?
Here is my code.
First the transaction class:
public class Transaction
{
// Class properties
private decimal amount;
private string type;
private decimal balance;
private string date;
private string transNum;
private string description;
// Constructor to create transaction object with values set.
public Transaction(decimal amount, string type, decimal currBal, string date, string num, string descrip)
{
this.amount = amount;
this.type = type;
this.balance = currBal;
this.date = date;
this.transNum = num;
this.description = descrip;
}
// Get and Set accessors to allow manipulation of values.
public decimal Amount
{
get
{
return amount;
}
set
{
amount = value;
}
}
public string Type
{
get
{
return type;
}
set
{
type = value;
}
}
public decimal Balance
{
get
{
return balance;
}
set
{
balance = value;
}
}
public string Date
{
get
{
return date;
}
set
{
date = value;
}
}
public string TransNum
{
get
{
return transNum;
}
set
{
transNum = value;
}
}
public string Description
{
get
{
return description;
}
set
{
description = value;
}
}
public decimal addCredit(decimal balance, decimal credit)
{
decimal newBalance;
newBalance = balance + credit;
return newBalance;
}
public decimal subtractDebit(decimal balance, decimal debit)
{
decimal newBalance;
newBalance = balance - debit;
return newBalance;
}
}
}
Now the code for the "Register" form:
public partial class Register : Form
{
List<Transaction> tranList = new List<Transaction>();
public Register(List<Transaction> List)
{
InitializeComponent();
this.tranList = List;
}
private void Register_Load(object sender, System.EventArgs e)
{
//regView represents the Datagridview that I'm trying to work with
regView.AutoSize = true;
regView.DataSource = tranList;
regView.Rows.Add(tranList[0]);
}
}
And here's the output I get.
There's really two high level approaches to this.
1) Add the manually created rows directly to the DataGridView. In this case, you have to manually update/remove them as things change. This approach is "ok" if you don't intend to alter/change the content of the display after you initialize it. It becomes untenable if you do.
To add it directly, you need to create a DataGridViewRow, and populate it with the individual values, and then add the DataGridViewRow to the DataGridView.Rows.
2) Data bind the DGV. There's many articles about databinding to a DataGridView. In some cases, it's easier to just add your data to a DataTable, and then extract a DataView from that, and bind the DataGridView to the DataView. Other people find it easier to directly bind to a collection.
CodeProject has a decent article to get you started down that path, but a quick Google search will yield many other articles.
http://www.codeproject.com/Articles/24656/A-Detailed-Data-Binding-Tutorial
use as DGV:
DataGridView groupListDataGridView;
column:
DataGridViewTextBoxColumn groupListNameColumn;
column setup should be like this:
groupListNameColumn.DataPropertyName = "name";
use this property, else all columns will be added.
groupListDataGridView.AutoGenerateColumns = false;
populate like this:
private void populateGroupList() {
groupListDataGridView.DataSource = null;
formattedGroupList = new SortableBindingList<DataGridGroupObject>();
foreach (GroupObject go in StartUp.GroupList) {
DataGridGroupObject dggo = new DataGridGroupObject();
dggo.id = go.Id;
dggo.name = go.Name;
formattedGroupList.Add(dggo);
}
groupListDataGridView.DataSource = formattedGroupList;
groupListDataGridView.Invalidate();
}
and model:
public class DataGridGroupObject
{
public int id { get; set; } //this will be match id column
public string name { get; set; } // this will be match name column
}
Simply add using System.Linq; at the top. Then you can do this:
//This will create a custom datasource for the DataGridView.
var transactionsDataSource = tranList.Select(x => new
{
Amount = x.amount,
Type = x.type,
Balance = x.balance,
Date = x.date,
TransNum = x.transNum
Description = x.description
}).ToList();
//This will assign the datasource. All the columns you listed will show up, and every row
//of data in the list will populate into the DataGridView.
regView.DataSource = transactionsDataSource;

Windows forms pass List to a new form

How can I pass a variable to another form?
I created the following class:
class Cart
{
private string productName;
private int qtd;
private decimal price;
public decimal Price
{
get
{
return price;
}
set
{
price = value;
}
}
public string ProductName
{
get
{
return productName;
}
set
{
productName = value;
}
}
public int Qtd
{
get
{
return qtd;
}
set
{
qtd = value;
}
}
}
I have one form that I add values to my cart:
public partial class frmProducts : Form
{
List<Cart> cartList = new List<Cart>();
private void btnAddCart_Click(object sender, EventArgs e)
{
if(txtQtd.Text == "")
{
MessageBox.Show("Enter how many items do you want.", "Products", MessageBoxButtons.OK);
return;
}
if (Convert.ToInt32(txtQtd.Text) > Convert.ToInt32(lblQtd.Text))
{
MessageBox.Show("We onlye have " + lblQtd.Text + " items in stock.", "Products", MessageBoxButtons.OK);
return;
}
Cart cart = new Cart();
cart.ProductName = lblProductName.Text;
cart.Qtd = Convert.ToInt32(lblQtd.Text);
cart.Price = Convert.ToDecimal(lblPrice.Text);
cartList.Add(cart);
}
}
I haave another WindowsForms that will work with the cartList. How can I send the cartList to the new WindowsForms?
Let frmProcessCart be the next form where you need the cartList to proceed. For that you can use any of the following options:
Get List<Cart> in the Constructor of that form:
Which means you have to pass the cartList as to the new form through its constructor, so you will get the same instance of the list their and you can proceed with that as well. In this case the Constructor of that form looks like this:
public frmProcessCart(List<Cart> cartList)
{
// Something here if needed
}
Another option is make cartList as static field:
In this case you can access the cartList from any other forms in the applications through frmProducts.cartList, you need not to pass any instance or create any instance of the frmProducts. in this case the definition of the cartList will be like this
public partial class frmProducts : Form
{
public static List<Cart> cartList = new List<Cart>();
// Rest of code here
}
In the new windows form declare a property for the cartList. Set the property before you show that form. Then your new form can work with that property.

Passing parameter value to a new form

I would like to point to a row.
Get the Oid(the parameter I want to pass).
When I click a button on the ribbon. It will open MifarePasswordForm. I would like to pass Oid to MifarePasswordForm so that the Oid can be saved in Mifare Card but I'm stuck at getting the Oid. So far, this is what I've got.
public void barButtonItem1_ItemClick()
{
staff entity = gridView.GetRow(gridView.GetSelectedRows()[0]) as staff;
entity.Oid;
MifarePasswordForm modalForm = new MifarePasswordForm();
modalForm.ShowDialog();
RefreshData();
}
This is my password form.
public MifarePasswordForm(int _iD)
{
InitializeComponent();
int iD = _iD;
}
Updated code
public void barButtonItem1_ItemClick()
{
staff entity = gridView.GetRow(gridView.GetSelectedRows()[0]) as staff;
MifarePasswordForm modalForm = new MifarePasswordForm(entity.Oid);
modalForm.ShowDialog();
RefreshData();
}
public MifarePasswordForm(int _iD)
{
InitializeComponent();
int iD = _iD;
textBox1.Text += iD;
}
What you can do is, pass your parameter to form in the constructor itself OR, make a public property and access it after creating formInstance and assign it your designated value.
e.g.
MifarePasswordForm modalForm = new MifarePasswordForm(entity.Oid);
modalForm.ShowDialog();

C# ComboBox List<object> ==> Show always the same object.name (multiple time)

I just want my ComboBox to show me the
FullName of objects in List(Curator),
but it show me the same "object.FullName" multiple times :-(
-
Basically, it work cause it show me the FullName of ONE of the Curator,
and the good amount of times,
but it show me the same ONE !
public partial class SGIArt : Form
{
public static Gallery gal = new Gallery(); // from a dll i made
List<Curator> curList = new List<Curator>();
public SGIArt()
{
InitializeComponent();
comboCur.DataSource = curList;
comboCur.ValueMember = null;
comboCur.DisplayMember = "FullName";
UpdateCurList();
}
public void UpdateCurList()
{
curList.Clear();
foreach (Curator cur in gal.GetCurList())
// from the same dll : Curators curatorsList = new Curators();
{
curList.Add(cur);
}
}
private void comboCur_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboCur.SelectedValue != null)
{
//show info in textBox (that work fine)
}
}
}
Curator class :
public class Curator : Person
{
private int id;
private double commission;
const double commRate = 0.25;
private int assignedArtists = 0;
public int CuratorID
{
get
{
return id;
}
set
{
id = value;
}
}
...
public Curator()
{
}
public Curator(string First, string Last, int curID)
: base(First, Last) // from : public abstract class Person
{
id = curID;
commission = 0;
assignedArtists = 0;
}
Edit: You might be looking for this answer.
I do not see the FullName member in your code snippet. I think you are looking for something like this:
List<Curator> curList = new List<Curator>();
public SGIArt()
{
InitializeComponent();
comboCur.DataSource = datasource;
comboCur.ValueMember = null;
comboCur.DisplayMember = "FullName";
UpdateCurList();
}
List<string> datasource()
{
List<string> datasource = new List<string>();
foreach(Curator curator in curList)
{
datasource.Add(curator.FullName)//this assume FullName is an accesible member of the Curator class and is a string.
}
return datasource;
}
The comboBox shows you object.FullName, because this is what you are telling it. The curList is empty at the time when you bind it.
You can update your list before using it:
public SGIArt()
{
InitializeComponent();
UpdateCurList();
comboCur.DataSource = curList;
comboCur.ValueMember = null;
comboCur.DisplayMember = "FullName";
}

Passing a true value to a boolean

I am trying to learn C# and I am up to an example that uses a boolean. For the life of me I cant figure out why the program isnt noticing that I am trying to pass a value of true to the boolean. Here is the code in the Form.cs:
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
HappyBirthday birthdayMessage = new HappyBirthday();
string returnedMessage;
birthdayMessage.PresentCount = 5;
birthdayMessage.MyProperty = "Adam";
birthdayMessage.hasParty = true;
returnedMessage = birthdayMessage.MyProperty;
MessageBox.Show(returnedMessage);
}
}
}
Here is the Class that I created:
class HappyBirthday
{
//====================
// CLASS VARIABLES
//====================
private int numberOfPresents;
private string birthdayMessage;
private bool birthdayParty;
//===========================
// DEFAULT CONSTRUCTOR
//===========================
public HappyBirthday()
{
numberOfPresents = 0;
//birthdayParty = false;
}
//===========================
// METHOD
//===========================
private string getMessage(string givenName)
{
string theMessage;
theMessage = "Happy Birthday " + givenName + "\n";
theMessage += "Number of presents = ";
theMessage += numberOfPresents.ToString() + "\n";
if (birthdayParty == true)
{
theMessage += "Hope you enjoy the party!";
}
else
{
theMessage += "No party = sorry!";
}
return theMessage;
}
//================================
// READ AND WRITE PROPERTY
//================================
public string MyProperty
{
get { return birthdayMessage; }
set { birthdayMessage = getMessage(value); }
}
//================================
// WRITE-ONLY PROPERTY
//================================
public int PresentCount
{
set { numberOfPresents = value; }
}
public bool hasParty
{
set { birthdayParty = value; }
}
}
Now I set the initial value to false (even though if my understanding is correct that should be the default value), but when I try to set it = true, the program does not recognize it. Am I supposed to pass a boolean differently then I would a string or int?
You're setting MyProperty before you're setting hasParty. getMessage() is not being called every time MyProperty is polled.
The way MyProperty works is confusing, because the set and get deal with different values (you set the name, and then get the whole message, which is confusing). I'd replace it with a GivenName property and then make the GetMessage() (or expose it as a read-only property Message) public.
Also, you can make your code much simpler by using auto-properties (you can use private gets to keep the write-only behavior, though in the real world write-only properties are very rare, and you should probably just make them public like the sets). And since the default int value is 0, you don't need to specify your default constructor. Here's how the code looks now:
class HappyBirthday
{
public string Message
{
get
{
string theMessage;
theMessage = "Happy Birthday " + GivenName + "\n";
theMessage += "Number of presents = ";
theMessage += PresentCount.ToString() + "\n";
if (HasParty)
{
theMessage += "Hope you enjoy the party!";
}
else
{
theMessage += "No party = sorry!";
}
return theMessage;
}
}
public string GivenName { private get; set; }
public int PresentCount { private get; set; }
public bool HasParty { private get; set; }
}

Categories