There are some controls in Form1 and a button named Filter. After the event Filter_Clicked is fired, the second form Form2 opens. There are some input controls in Form2 and a button named Search. After the Search_Clicked event is fired, the Form2 sends a query to one of the methods in Form1. Then the result is displayed in a GridView control of Form1.
My problem is that:
I defines row data for each column, but when the result is displayed in the gridview, there is only column's name to see and row are empty even not display as NULL.
I have tried calling Form2 from Form1 in two different ways and each one causes a different result and warning:
Variant 1:
/* Result: Display only column name in Form1 gridview */
public Form2()
{
InitializeComponent();
var way1 = Application.OpenForms.OfType<Form1>().Single();
}
The Search_Clicked() event looks like this:
String searchExam = comboBoxForExamFilter.SelectedItem.ToString();
String searchFilter = "select Exam_Name,Subject_Name,Marks,Status from ABC_SchoolDB.dbo.Sheet_Marks where Exam_Name= ' " + searchExam + " ' ";
way1.userSqlQueryExecutor(searchFilter);
Note:
when calling userSqlQueryExecutor(searchQuery) from Form1, it displays the table data
Don't worry about sql injection
Variant 2:
/* Result: NullReferenceException was unhandled */
private Form1 way2;
public Form2(Form callForm1)
{
InitializeComponent();
way2 = callForm1 as Form1;
}
The Search_Clicked() event looks like this:
way2.userSqlQueryExecutor(searchFilter);
How do i show all my row data in the GridView control?
Related
I have a c# windows form app written in .net-4.0
I have a datagridview (dgvItems) which I programmatically bind to a dataset and datatable at the end of an import function.
I have an import button that imports data from a selected excel file into a table called _dtItemAdjustList, once the data has been imported I bind that table to my grid. The code I use to bind the grid is:
dgvItems.DataSource = _dsQtyAdjust;
dgvItems.DataMember = "Item Adjust List";
dgvItems.Refresh();
Everything works fine so far, if I edit the imported data in the datagridview, it updates the bound table _dtItemAdjustList after each cell is edited.
My issue comes in when I try to manually add a row to my datagridview, it doesn't immediately add that new row to the bound datatable.
Example: I put a break point at the end of my dgvItems_CellValueChanged event and added a _dtItemAdjustList.Rows.Count watch and here is what happens.
After the data is imported and I edit one of the existing imported lines the watch shows the correct row count, lets say 5.
Now I click the last * row, and type something into my item# column and hit tab, the break point fires but my row count still only shows 5, I fill out a few more cells in the new row but each time I leave a cell and my CellValueChanged event fires and the row count remains at 5.
Next I add a second manual row, now immediately after I tab out of the first cell I filled out for this second new row, my row counter goes to 6, but technically at this point I've added 2 manual rows to my imported 5, so the counter should read 7
This repeats, basically each new manually added row to the datagridview isn't added to the bound datatable until either a) another row is added or b) I go back and re-edit a cell on an existing row.
Edit
Forgot to mention I tried binding my DataGridView two ways:
DataSource = _dsQtyAdjust //dataset Name
DataMember = "Item Adjust List" // Table Name in the dataset
DataSource = _dtItemAdjustList
It made no difference as far as my new row behaviour goes.
Ok after a lot of trial and error and online searching I found a solution that seems to work.
Instead of binding my DataGridView directly to my DataTable, I created a BindingSource, bound the BindingSource to my DataTable, then bound my DataGridView to the BindingSource and finally in my dgvItems_CellValueChanged event I used a combination of EndEdit() and NotifyCurrentCellDirty(true/false) to make it work.
Code sample:
public partial class frmQuantityAdjustment : Form
{
// In my forms partial class I created a new public BindingSource
public BindingSource bsItemAdjust = new BindingSource();
}
private void frmQuantityAdjustment_Load(object sender, EventArgs e)
{
// In my form_Load event I bound the BindingSource to my DataTable
// and then the DataGridView to the BindingSource
bsItemAdjust.DataSource = _dtItemAdjustList;
dgvItems.DataSource = bsItemAdjust;
dgvItems.Refresh();
}
private void dgvItems_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
// Finally at the end of my CellValueChanged event I added the
// following code
bsItemAdjust.EndEdit();
dgvItems.NotifyCurrentCellDirty(true);
dgvItems.EndEdit();
dgvItems.NotifyCurrentCellDirty(false);
}
I came across this solution on this thread and after some testing it seems to work perfectly.
Now the new row in my DataGridView is added to the bound DataTable at the end of the CellValueChanged event.
I have a Windows Forms application which contains a DataGridView inside it. The DataGridView gets populated from a DataSource which is a list of MyClass. MyClass contains a set of properties and a constructor like so:
public class MyClass
{
public PropertyA{get;set};
public ProppertyB{get;set;}
public ProppertyC{get;Set}
}
Then in the Main form I have a method which returns a List myCollection and a button which populates the DataGridView with "myCollection" like so:
private void btlLoadDataInDataGrid_Click(object sender, EventArgs e)
{
var headers = GetAllHeaders();//GetAllheaders returns a List<MyClass>
dataGridView1.DataSource = headers;
}
What I get is a DataGridView with the columns from the original MyClass.
Now I want to be able to edit the data in the DataGridView and commit this change to the actual list of MyClass properties. Can somebody please advice what would be a best approach to do this?
When you edit your grid whose DataSource is set to a List of objects and change any cell's value, the underlying data in the row is automatically updated. In your case the underlying row data is of type MyClass.
At any point of time you can get the updated row data, using the DataGridView.DataBoundItem property.
Sample code:
foreach(DataGridViewRow item in dataGridView1.Rows)
{
MyClass rowData = item.DataBoundItem as MyClass;
if(rowData != null)
{
// Do your stuff
}
}
The data commit logic is different when your grid is populated from a database. You have to write code to commit the changed data to the database.
I am a beginner in WPF and trying to learn,So Sorry for this type of query.
I have added a property named GetName in WPF Form1 as below:
public string GetName
{
get { return this.uname.Text; }
set { this.uname.Text = value; }
}
Trying to retrieve the GetName property in WPF Form2 as below:
public Home()
{
InitializeComponent();
Form1 mn = new Form1 ();
MessageBox.Show(mn.GetName.ToString());
}
But I am getting prompt with blank. What I am doing wrong here. Please help.
You create a new Form:
Form1 mn = new Form1 ();
Then you request the Text value of what I assume is a TextBox control to be shown in a MessageBox:
MessageBox.Show(mn.GetName.ToString());
I would not expect GetNameto return a value, as when a TextBox control is created, unless specified, it will not have a value.
Between creating the Form and showing the MessageBox, you do not show your Form, for example:
mn.Show();
You need to show your Form first, input a value into your uname control and then request its value.
Calling ToString() on GetName is redundant, just use:
MessageBox.Show(mn.GetName);
I have 2 forms : Form1 and Form2
Form1 start running with Application.Run()
and Form1 have a datagridview if user Start Edit the DataGridView
i run Form2 like this in CellBeginEdit event
Form2 addStock = new Form2(productNo, stockString, this);
addStock.Activate();
addStock.Show();
After Form2 finish the task, When i click a button on Form 2 i want to get value return to Form1 and show in the DataGridView on Form1 and close the Form2
i tried to pass the Form1's object
Form2 addStock = new Form2(productNo, stockString, this);
but on Form2 i can't access the Form1's DataGridview with that object
how can i do to accomplish this?
what i suggest made a custom class and make some static members and when you are navigating to form2 jux check them.
class CustomClass
{
public static int ProductNo;
public static string StockString;
}
//these are call to form2 from form1
CustomClasss.ProductNo=12; //some value
CustomClass.StockString="Some Value";
Form2 addStock = new Form2();
addStock.Activate();
addStock.Show();
now call these custom class members in form 2 and validate according to ur need..
like
if(CustomClass.ProductNo==2 && CustomClass.StockString=="Some String")
{
//do something
}
Hope it helpx..
Add public properties to Form2. After finishing the computation whatever you are doing within Form2 set the values to those properties. Now these values are accessible within From1 with the same From2 instance you have created to show the Form2.
Now within From1 access those property values of Form2.
Form2 addStock = new Form2(productNo, stockString, this);
addStock.Activate();
addStock.Show();
//Reading the property value of Form2
string stockValue = addStock.StockStringProperty;
I have trouble editing a databound bindinglist. Let me illustrate it with the following:
Say I have the Person class:
public Class Person{
private string m_firstname;
private string m_lastname;
public string FirstName{get;set;}
public string LastName{get;set;}
public Person{ ... }
}
I then have a containing class called Population:
public class Population{
private BindingList<Person> m_lstPerson = new BindingList<Person>();
private string m_countryName;
public BindingList<Person> ListPerson{get; set;}
public string CountryName { get; set; }
}
I then have on one form a first datagridview with DataSource = m_lstPopulation (BindingList). The binding works like a charm when working with the Population objects. When I double click, it opens up a dialog form showing the object details. One tab in the details holds a datagridview bound to that population's ListPerson.
The second datagridview displays fine. However, I cannot edit or add cells in this datagridview. None of the columns is set to read-only. In fact, both datagridview have just about the same parameters.
What am I missing? It seems that a lock has been placed on the Population object so that its inner fields cannot be edited...
Please advise. Thanks.
First verify that these grid properties are set:
ReadOnly = false;
AllowUserToAddRow = true;
EditMode = ;
If that doesn't work then you may be getting stuck in edit mode... It sounds like you have some custom behavior on your grid ("When I double click, it opens up a dialog form showing the object details.")...
For this try calling DataGridView.CancelEdit() after your dialog closes to end the edit session on the clicked row. This will restore the "new row" row to the grid. It disappears when you begin editing another row, which, depending on the EditMode setting may begin when you click on (enter) another row.