Hi again people,
Who knows how to show value of the selectedRow in DataGridView on a messageBox?
Thanks in advance.
Hi again people,
Who knows how to show value of the selectedRow in DataGridView on a messageBox?
Thanks in advance.
Do you want to show the whole row or a cell value of a column from the selected row?
To show a particular value from the selected row would be something like: DataGridView1.SelectedRows[0].Cells[1].Value
HTH
When i select a row, i want also the FullName... but i have a object.
want to get value of property(FUllName) of my object when select a row in datagridview
Do you mean the column title and the type of the column?
I have a object:
public class CandidateDataRow : DataRow
{
private string _c { get; set; }
internal CandidateDataRow(DataRowBuilder b)
: base(b)
{
FullName = string.Empty;
YearsOfExperience = string.Empty;
Job = string.Empty;
Certification = string.Empty;
Email = string.Empty;
}
public string FullName { get; set; }
public string YearsOfExperience { get; set; }
public string Job { get; set; }
public string Certification { get; set; }
public string Email { get; set; }
}
public class CandidateTable : DataTable
{
public CandidateTable()
{
Columns.Add(new DataColumn("FullName", typeof(string)));
Columns.Add(new DataColumn("YearsOfExperience", typeof(string)));
Columns.Add(new DataColumn("Job", typeof(string)));
Columns.Add(new DataColumn("Certification", typeof(string)));
Columns.Add(new DataColumn("Email", typeof(string)));
}
public CandidateDataRow NewRow(CandidatesForJob c)
{
CandidateDataRow row = (CandidateDataRow)NewRow();
return row;
}
and my principal form:
public partial class Form1 : Form
{
//Declare BindingList<> object of type CandidatesDetails class
//List<CandidatesForJob> CandidatesDetails = new List<CandidatesForJob>();
CandidatesForJob cfj = new CandidatesForJob();
DataSet ds = new DataSet();
DataTable dtable = new DataTable("Candidates");
//DONE
public Form1()
{
InitializeComponent();
}
//DONE | FORM LOAD
private void Form1_Load(object sender, EventArgs e)
{
//em testes
dtable.Columns.Add("FullName", typeof(string));
dtable.Columns.Add("YearsOfExperience", typeof(int));
dtable.Columns.Add("Job", typeof(string));
dtable.Columns.Add("Certification", typeof(string));
dtable.Columns.Add("Email", typeof(string));
dataGridView1.DataSource = ds.Tables[0];
dataGridView1.AutoGenerateColumns = true;
}
//DONE | Close
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
AddCandidate MyForm = new AddCandidate();
if (MyForm.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
CandidatesForJob _c = MyForm._resultado;
CandidateTable _ct = new CandidateTable();
CandidateDataRow _cr = _ct.NewRow(_c);
DataRow nr = ds.Tables[0].NewRow();
nr["FullName"] = _c.FullName;
nr["YearsOfExperience"] = _c.YearsOfExperience;
nr["Job"] = _c.Job;
nr["Certification"] = _c.Certification;
nr["Email"] = _c.Email;
ds.Tables[0].Rows.Add(nr);
XElement xmlNode = new XElement("Candidate",
new XElement("FullName", _c.FullName ),
new XElement("YearsOfExperience", _c.YearsOfExperience ),
new XElement("Job", _c.Job ),
new XElement("Certification", _c.Certification ),
new XElement("Email", _c.Email ));
XElement xmlFile;
try
{
xmlFile = XElement.Load("diogomonteiro.xml");
xmlFile.Add(xmlNode);
}
catch (XmlException)
{
xmlFile = new XElement("CandidatesForJob", xmlNode);
}
xmlFile.Save("diogomonteiro.xml");
}
}
I want "FullName" of my object (ex: DaniWeb) when i double click on Row in DatagridView..
Hi
If you override the ToString method in your CandidateDataRow class you can have that return the FullName property:
public override string ToString()
{
return FullName;
}
Alternatively, cast the value you get from the SelectedRow to a CandidateDateRow and access any of the properties that you need.
HTH
Its all i need " cast the value you get from the SelectedRow to a CandidateDateRow and access any of the properties that you need."
Can u help me do that? im a beginner
Thanks
To cast something you would do (type)(what to cast). So to cast a string that contained "123" to an integer would be (int)(theStringVariable)
In your case (and not having a look at all of your code) you might do (CandidateDataRow)(DataGridView1.SelectedRows[0])
However, I'm not sure how you are binding to the DataGridView so the above may need some more work.
What do you get back now in the message box when you use the initial code I posted?
with the initial code u posted, i get a error before debug:
Error 1 Only assignment, call, increment, decrement, and new object expressions can be used as a statement .
How were you using the initial code? Can you show us?
private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
((CandidateDataRow)(dataGridView1.SelectedRows[0]));
}
With the initial code i get the same error.
You need to assign it to something, for example CandidateDataRow row = .....
another error:
private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
CandidateDataRow row = dataGridView1.SelectedRows[0].Cells[1].Value;
}
error: Error 1 Cannot implicitly convert type 'object' to 'MateusCandidatos.CandidateDataRow'. An explicit conversion exists (are you missing a cast?)
A row is of type CandidateDataRow
you cannot give it a Value
.
Have not tested but I would try this:
CandidateDataRow row = dataGridView1.SelectedRows[0];
MyWhatEver = row.Cells[1].Value;
private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
CandidateDataRow row = dataGridView1.SelectedRows[0];
CandidatesForJob = row.Cells[1].Value;
}
3 Errors:
Error 3 'MateusCandidatos.CandidateDataRow' does not contain a definition for 'Cells' and no extension method 'Cells' accepting a first argument of type 'MateusCandidatos.CandidateDataRow' could be found (are you missing a using directive or an assembly reference?)
Error 2 'MateusCandidatos.CandidatesForJob' is a 'type' but is used like a 'variable'
Error 1 Cannot implicitly convert type 'System.Windows.Forms.DataGridViewRow' to 'MateusCandidatos.CandidateDataRow'
What I meant was something like:
CandidateDataRow row = (CandidateDataRow)dataGridView1.SelectedRows[0]
However, I am wondering what that would return so before trying can you output the value of: dataGridView1.SelectedRows[0].toString() to a MessageBox and tell us what it is.
private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
CandidateDataRow row = (CandidateDataRow)dataGridView1.SelectedRows[0];
MessageBox.Show(dataGridView1.SelectedRows[0].toString());
}
2 errors:
Error 2 'System.Windows.Forms.DataGridViewRow' does not contain a definition for 'toString' and no extension method 'toString' accepting a first argument of type 'System.Windows.Forms.DataGridViewRow' could be found (are you missing a using directive or an assembly reference?)
Error 1 Cannot convert type 'System.Windows.Forms.DataGridViewRow' to 'MateusCandidatos.CandidateDataRow'
with MessageBox.Show(dataGridView1.SelectedRows[0].toString());
i get DataGridViewRow { Index=8 }
Ok, let's start again
What do you get with MessageBox.Show(dataGridView1.SelectedRows[0].Cells[1].Value.ToString())
Bear in mind that it may not be cell 1 it might be 0
with Cells[1] when i double click in row i get the value of YearsOfExperience property, but with Cells[0], i get the value of Full Name..but what I want is to use my object with the variable "_c".. Edit: to do that my boss sad me what u said " Cast the value i get from the SelectedRow to a candidateDataRow and get access any of the properties that i need
I could be wrong but having looked again at your original code you are not binding your object to the DataGridView but instead binding a DataTable which you build up. On this basis you don't have access to the object from the DataGridView.
So, i have to bind my object to my datagrid view? how?
Thanks.
Well you could create a List of Candidate Rows and when you get the result back from your other form add a new CandidateRow to that list. You would then use the List as the DataSource for the DataGridView
Have a look into binding list of objects.
but i dont want that.. today i have this code
private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
//CandidateDataRow row = (CandidateDataRow)dataGridView1.SelectedRows[0];
MessageBox.Show(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
DataRowView x = (DataRowView)dataGridView1.SelectedRows[0].DataBoundItem;
DataRow dr = x.Row;
when i double click on a row , i get the FullName property value on a msgbox..
but i want to associate the DataRow to my object CandidateDataRow.. because my object inherits public class CandidateDataRow : DataRow
I must admit, I am somewhat confused with your code.
You have this CandidateDataTable and CandidateDataRow but you are binding a Data Table to your DataGridView so they are not being used for binding.
You also use the DataRowBuilder in your CandidateDataRow class which is not supported. This class is meant to support the framework and not for use within your own applications.
Without knowing fully how your application works, it is difficult to advise appropriately. However, I modified your CandidateRow class:
public class CandidateDataRow
{
private string _c { get; set; }
public string FullName { get; set; }
public string YearsOfExperience { get; set; }
public string Job { get; set; }
public string Certification { get; set; }
public string Email { get; set; }
}
so that it becomes a simple class. This broke the CandidateTable as it was setting up a NewRow but I simply commented this out. Then I used the following code in the double click event to build a new CandidateRow:
DataRowView x = (DataRowView)dataGridView1.SelectedRows[0].DataBoundItem;
DataRow dr = x.Row;
CandidateDataRow row = new CandidateDataRow
{
FullName = dr["FullName"].ToString(),
YearsOfExperience = dr["YearsOfExperience"].ToString(),
Job = dr["Job"].ToString(),
Certification = dr["Certification"].ToString(),
Email = dr["Email"].ToString()
};
which successfully creates a CandidateDataRow with the data passed from the DataRow retrieved from the SelectedRow of the DataGridView.
Now, I am sure that this is not what you are after as other areas of your code may rely on these classes, but it does highlight the fact that by making these changes has not affected the workings of the original code you posted with regards to the DataGridView.
Can we chat in real time ?
I'd rather keep all help on the Forum so that others can benefit from the solution. Also, I am at work at the moment so chatting is not really permitted and posting on forums is a little more discrete from my perspective.
Yeah, that's right.. but my own chat is discrete, visit me on mateus.work/chat
Big Thanks to djjeavons, that helped me a lot to solve this problem and Recommended me to use other methods that facilitated the reading and the operation of the application.
With this code on MouseDoubleClick, when write "row." i can get properties.
//DJ - I have used the CandidatesForJob object rather than the CandidateDataRow
CandidatesForJob row = new CandidatesForJob()
{
FullName = dr["FullName"].ToString(),
YearsOfExperience = dr["YearsOfExperience"].ToString(),
Job = dr["Job"].ToString(),
Certification = dr["Certification"].ToString(),
Email = dr["Email"].ToString()
};
MessageBox.Show(row.FullName + ", " + row.Email);
// DJ - Moved the CandidateTable class to the top of the code file so that you do not get design time errors when opening the object.cs in the designer.
public CandidateTable()
{
Columns.Add(new DataColumn("FullName", typeof(string)));
Columns.Add(new DataColumn("YearsOfExperience", typeof(string)));
Columns.Add(new DataColumn("Job", typeof(string)));
Columns.Add(new DataColumn("Certification", typeof(string)));
Columns.Add(new DataColumn("Email", typeof(string)));
}
in form1.cs :
//DJ - Renamed the table from Candidates to Candidate to match the XML file.
DataTable dtable = new DataTable("Candidate");
Thanks Again. Solved
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.