Getting values from gridView cell C# - c#

I want to get the data from a cell from a selected row, in a gridView, and bring it into another form, inside a textEdit that then edit the row of the selected cell, the problem is that when using
var cacca = this.gridView.getFocusedRow() as PartnersMissing;
MessageBox.show(cacca.toString())
the messageBox writes Test01.parnersMissing, where Test01 is the name of the project and I really don't know why PartnersMissing is there. What is wrong in this code?
The datagrid has data from an sql database and it is made with DevExpress.
Please don't mind the names for the variables if you know italian, I was in a hurry :D

The current row can be obtained using the GridView's
GetFocusedRow method. This method returns an object whose actual
type is determined according to the data source.
If you are binding it with a List<PartnersMissing> objects then you are getting correct value from the GetFocusedRow method. If you want to access the property of the current row object then you can simply do it.
for example: PartnersMissing has property ABC then you can access as below:
var cacca = this.gridView.getFocusedRow() as PartnersMissing;
MessageBox.show(cacca.ABC)
ToString() method returns the type of the object not any property value. you just printing type of the object rather than accessing property of object.
As Dimitry Says, You can also access particular column value or row' PartnersMissing object property using the GetFocusedRowCellValue(String) Method.

As far as I can see from your code, the result is expected because the cacca.toString() expression return the type name for those types, which do not override the standard object.ToString method.
To show something helpful, you should either to override this method
class PartnersMissing {
public string Name { get; set; }
public override string ToString(){
return Name;
}
}
or to obtain the specific values from your object's fields:
MessageBox.Show(cacca.Name);
You can also to obtain the object's fields value directly from the gridView:
string name = gridView.GetFocusedRowCellValue("Name");

Related

Add a column with derived data to a DataGridView

I have a datagridview bound to a list of "CarColor" objects. I am attempting to add an extra column to the dataGridView that takes the Name property of each CarColor and displays the localized version.
Currently I have an extension method that creates the column and populates it with the correct values but every time the dataGridView changes visibility or data, all the cells in the Localized Name column become empty. Before now I've managed a workaround by running the extension method on the dataGridView's VisibileChanged and DataSourceChanged events. This is cumbersome, especially if I am trying to affect a DataGridView in another control (such as a dialog).
I have read that using a DataGrid is an option, via setting the "Expression" value of a new column. However, I don't know if it's possible to convert a list to a DataGrid, or convert a DataGridView into a DataGrid.
How can I ensure that the values of the Localized Name table aren't erased?
If you have a List<CarColor> you don't need DataTable to add a new computed column. You can simply add a new property to the CarColor that returns the desired computed value or in your case the localized value of its Name property.
public partial class CarColor
{
publlic string LocalizedName
{
get
{
return GetLocalizedName(this.Name);
}
}
private string GetLocalizedName(string name)
{
// put the logic for localizing the name here
}
}
Note:
I wrote it partial, so you can let the original CarColor class untouched, just add this partial in the same namespace, or if you prefer, simply add body code of the class to the original CarColor.
If CarClass in not your class and you van not change it's code, yo can create a ViewModel class for CarColor and use the same idea to represent that property.

Mutator method not changing TextBox text to parameter contents

I have a series of mutator methods in another class, each of which is linked to a textBox.
(ClassA). Now, I am using an object of ClassA (myClassAObject.setFirstName(param here), to set the text, so the contents of that parameter will display in that textbox.
The content of that mutator method is as follows:
public void setFirstName (string newFirstName)
{
txtBoxFirstName.Text = newFirstName;
}
I know that the mutator works correctly, because otherwise I'd not see the first name of the patient, I'd see "null", or in the case of a println, a blank space.
The question is below this line
The mutator method, for some reason is not causing the textBox to display the text it is given in a parameter, when I access it from the other class. The textBox remains blank. How can I get it to work correctly? I have used MessageBoxes to check if there is a value for the textBox to display, and yes, the contents of the variable appear onscreen.
Here is how the parameter is passed from the second class, to the class in which the textBox resides:
myClassAObject.setFirstName(firstName);
The code above is example code, but it illustrates what I am doing.
Below is the actual code.
Firstly, from the class where the textBox resides.
Secondly, where the new parameter is being passed from.
public void setPatientFirstName(string newPatientFirstName)
{
txtBoxPatientFirstName.Text = newPatientFirstName;
}
Second part of code:
patientRecordClassOverseer.setPatientFirstName(patReaderFirstName);
I am using a "Reader" (OleDB) to read the data from the database. It is working, as I have MessageBoxes setup for debugging purposes.

DataGridView rows show up but no data

I am trying to bind a collection (that inherits from BindingList) to a DataGridView. The grid headers show up fine and the I get the number of rows that I expect. However, the cells are empty. Has anyone else had this problem? If so, how did you resolve it? I've talked to someone else who had the same problem but they can't remember how they resolved it. I've tried to create a simple version that shows the problem but haven't had any luck. So I'm sorry, I haven't included any code.
EDIT #1:
I don't think this code will help but just in case. I have simplified things in order to prevent from having to outline the 47 layers of our code. But like I said, I can't recreate the problem with a very simple example like this. I'm not really wanting code analysis--just words of wisdom from those who have run into this problem. Surely I'm not the only one.
public interface ISearchResultCollection : IList<ISearchResult>
{
...
}
public class SearchResultCollection : BindingList<ISearchResult>, ISearchResultCollection
{
...
}
public interface ISearchResult
{
ILineNum LineNumber {get; set;}
string Text {get; set;}
}
public class SearchResult
{
...
}
ISearchResultCollection results = objectToSearch.Find("searchstring");
dataGridView1.DataSource = results;
EDIT #2:
I think I've got a lead. All of the public properties on my interface that represents an item (ISearchResult) are interface types as well. I added a string property and its data is magically showing up. So, in the above example, the Text column's data would show up. But, the LineNumber column's data would not since it is of an interface type (ILineNum). I figured ToString() would be called to populate the grid on these. Any ideas now?
When you databind an object to some control, the databinding interfaces will automatically call "ToString()" on your object so you get a decent string representation in whatever control you are binding to. In the case of a DataGridView you will see the ToString'd representation of the underlying object in the appropriate cell. However, when you databind to an interface, no such call to ToString() is made. This means that your DataGridView will display an empty cell but this is quite misleading as the bound object is actually there as it should be - it just doesn't have any value to display in the cell.
To fix this you need to deal with the formatting of a display value yourself. In a DataGridView, this can be achieved by handling the CellFormatting event. This event is fired whenever any cell needs to format its value so you must be careful not to put too much heavy logic in this handler. In your case, all you need to do is call "ToString()" on the underlying object. So you can handle the CellFormatting event then use something like this in your handler:
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.Value != null && dataGridView1.Columns[e.ColumnIndex] == theRelevantColumn)
{
e.Value = e.Value.ToString();
}
}
In the above snippet we are checking for a specific column before performing the logic but this is optional, your own scenario may be different. The main point to note is that we get the original object passed in on the event args and all we need to do is set the formatted value back in the event args. In this case, the formatted value is generated by calling "ToString()" on the underlying object. In your DataGridView, you will now see this formatted value in the appropriate cell.
Hope that helps.
You need to set AllowUserToAddRows to false;

Getting Value from DataView C#

How to find a value of some column from DataView.CurrentItem.
As Paul pointed out in his comment, there is no CurrentItem member in the DataView class.
If you know the index of the item, you can access a column by its name as shown below :
string name = dataView[index]["Name"] as string;
Similarly, if you have an instance of a DataRowView (a view of a DataRow), you can do that :
string name = dataRowView["Name"] as string;
EDIT: I just noticed the WPF tag on your question... perhaps you're talking about a CollectionView, not DataView ?
CollectionView doesn't have "columns" per se, but it can be represented in a GridView or DataGrid (which both have columns). It's just a view over a collection of objects. To access a specific field or property of the current object, there are two main options :
if you statically know the actual type of the collection items : cast the CurrentItem to that type, and directly access the members you need
if you don't know the type, you can use reflection on the CurrentItem to access its properties or fields by name

DataGridView - setting object's displayed value in a column

I bind some collection to a DataGridView. Collection contains KeyValuePair objects where key is a string and value is an object of my class Field. DataGridView displays two columns - one containing the key and the other one containing the value. The value (Field object) is displayed with its ToString() method. But I would like it to be displayed using its Name property. The problem is the column contains no DisplayMember property.
How can i do it?
Edit: I know I could override ToString() to return the name of the object but I don't want to do that.
DataGridView (in common with most direct list-based bindings) can only bind to immediate properties of the row item. You could perhaps create a facade object for this? i.e. a class that accepts the instance and returns the name as a direct property:
public string Name {
get {return innerObject.Name;}
set {innerObject.Name = value;}
}
// snipped: other properties - Key etc
Alternatively, you could project into a new object? For example, data-bindings work (read-only, at least) with anonymous types pretty well:
grid.DataSource = originalData.Select(x=>
new {x.Key, Name = x.Field.Name}).ToList();
Finally, you can hack around in ComponentModel to flatten the model at runtime, but it really isn't worth it just for this.
You could put the DataGridView into virtual mode (view.VirtualMode = true), and handle the CellValueNeeded (and possibly the CellValuePushed) events to access the "Name" property. This would avoid creating lots of wrapper objects, but does make the code somewhat less elegant.

Categories

Resources