I have a program that uses a datagridview with 7 columns in it. One of the columns is a hyperlink that will load a file from a specified location. I use a 'cellcontentclick' event to open the file. My problem is, when I click any of the other cells in the row, it will still execute the cellcontentclick. How do I make it so only when that specific column will execute when clicked?
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
try
{
string sourcePath = #"SPECIFIED PATH";
Process.Start(sourcePath + dataGridView1.Rows[e.RowIndex].Cells[5].Value);
}
catch (SqlException e)
{
MessageBox.Show("Error occured: " + e);
}
}
Check inside event handler only for column you are looking for.
One of the parameters (e?) has column info.
Got it! I just needed to enter the if statement and specify the column. Thanks, evgenyl.
if (e.ColumnIndex == 5 && e.RowIndex >= 0)
{
try
{
string sourcePath = #"PATH";
Process.Start(sourcePath + dataGridView1.Rows[e.RowIndex].Cells[5].Value);
}
catch (SqlException a)
{
MessageBox.Show("Error occured: " + a);
}
}
Related
I have a method that foreaches a row within a datagridview and starts a process. However, when I click the toolstripmenuitem to execute the method it does not work. Any suggestions?
This is the toolstripmenuitem ^
private void sIGNATUREToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (Properties.Settings.Default.ManageContactsView == "STACKED")
{
ViewSignature(AllContacts);
}
else if (Properties.Settings.Default.ManageContactsView == "LISTED")
{
ViewSignature(AllContactsLISTED);
}
else
{
ViewSignature(AllContactsFULL);
}
}
This is the code that is placed in the menu items click property ^
void ViewSignature(DataGridView AllContacts)
{
foreach (DataGridViewRow row1 in AllContacts.Rows)
{
try
{
if (Convert.ToBoolean(row1.Cells[18].Value))
{
Process.Start(Properties.Settings.Default.ReferencePoint + #"/Contacts/Contact_" + row1.Cells[0].Value.ToString() + #"/Signature.jpg");
}
}
catch
{
//Error message if the system cannot remove the contact from the database.
MessageBox.Show("Sorry, something went wrong. Please try again later.\n\nERROR CODE: BINSPIRE1009", "BLUEBERRY INSPIRE 2022", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
This is the method I am trying to execute which fetches a column and row value from the datagridview and starts a process.
I have created a form to insert data. When data inserted it shows into datagridview1. I use dataGridView1_MouseClick event to retrieve data into the form. I successfully retrieved textbox data only into my form. I want to retrieve checkbox value which is checked or not. How can i retrieve checkbox value by clicking dataGridView1_MouseClick event.
I use following code for that:
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
try
{
txtID.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
Name.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
}
catch (ApplicationException ex)
{
MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
}
My result is here
But I want like this:
U can retrive values of other fields in similar like u did
This is for WPF control Checkbox link
I assume checkbox name of movie is movie_chbox
movie_chbox.IsChecked =
dataGridView1.SelectedRows[0].Cells[3].Value.toString() == "1" ?true:false
Similarly you can do same for other too.
This is for winforms control Checkbox link
movie_chbox.Checked =
dataGridView1.SelectedRows[0].Cells[3].Value.toString() == "1" ?true:false
Instead of displaying 0 or 1 in datagridview display "Interested"/"Not Interested" and when you insert/update to database that time insert/update as 0 or 1 for better database mentainance.
Use the checked property of the check box control in your code and change the code for different categories. In your case the final code will be be something like below:
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
try
{
txtID.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
txtName.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
string isCheckedMovie = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
string isCheckedSports = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
string isCheckedReading = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
string isCheckedWriting = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
if (isCheckedMovie == "1")
movieBox.Checked = true;
else
movieBox.Checked = false;
if (isCheckedSports == "1")
sportsBox.Checked = true;
else
sportsBox.Checked = false;
if (isCheckedReading == "1")
readingBox.Checked = true;
else
readingBox.Checked = false;
if (isCheckedWriting == "1")
writingBox.Checked = true;
else
writingBox.Checked = false;
}
catch (ApplicationException ex)
{
MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
What I am trying to accomplish is opening a new windows form with my sqlite data already populated when the student name is clicked, to edit or delete data.
How can I populate the data in the edit form when the specific row is clicked? The link to the Edit form is already created.
After any first name in the first name column is clicked, the following windows open.
I am assuming that has something to do with my cell content click function, how can I implement it?
private void dataGridView1_CellContentClick_2(object sender, DataGridViewCellEventArgs e)
{
string ExePath = (new System.Uri(Assembly.GetExecutingAssembly().CodeBase)).AbsolutePath;
string drive = Path.GetPathRoot(ExePath);
if (e.RowIndex == -1 || e.ColumnIndex == 0)
{
this.Hide();
editStudent editStudent = new editStudent();
editStudent.Show();
}
else
{
}
if (e.RowIndex == -1 || e.ColumnIndex != 6) //ignore header row and any column that doesnt have file name
return;
var fileName = #"" + drive + "WindowsFormsApplication1\\PDF\\" + dataGridView1.CurrentCell.Value.ToString() + "\\" + dataGridView1.CurrentCell.Value.ToString() + ".pdf";
if (File.Exists(fileName))
{
Process.Start(fileName);
}
else
{
MessageBox.Show("File Does Not Exist");
}
}
figured it out..
editStudent editStudent = new editStudent();
editStudent.firstName.Text = dataGridView1.CurrentRow.Cells["First Name"].Value.ToString();
editStudent.ShowDialog();
This is the code i used in the cell content click function.
Thanks everyone for the help.
I am saving my bound datagridview and on the event, I'm am saving the record with this method:
void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e)
{
try
{
Entity.SaveChanges();
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Error while saving. Check the current row for errors.{0}{0}Exception:{0}{0}{1}", Environment.NewLine, ex.Message));
// I'd like to push it back to the last cell edited here
}
}
If an exception occurred, I'd like to push the users focus back to the last cell edited. Is this possible?
Is there a way to do this?
string tbname;
void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e)
{
try
{
Entity.SaveChanges();
tbname = Entity.Accessible.Name; //save the name of the last edit textbox
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Error while saving. Check the current row for errors.{0}{0}Exception:{0}{0}{1}", Environment.NewLine, ex.Message));
tbname.focus(); //will return the focus to that textbox last edited
}
}
I'm a beginner with C# so I made a little app, to figure out how to work with Linq and databased in C#.
What I'm trying to do is in a DataGridView when someone clicks a row containing some data I want to go from e.RowIndex to a Linq object of the data in that row, my attempt involved using the DataBoundItem.
But for whatever reason the currentAd variable in this code always gives me a null value.
private void clickRow(object sender, DataGridViewCellEventArgs e)
{
richTextBox1.Text = "There is a clickRow event with row index " + e.RowIndex;
Ad currentAd = adsDataGridView.Rows[e.RowIndex].DataBoundItem as Ad;
if (currentAd != null) // The problem is it is always null
{
MessageBox.Show( currentAd.ToString() );
}
}
Thanks for your help.
In the examples given on the MSDN site, they cast the row to a DataGridViewRow object first, before taking the DataBoundItem, so you might want to try it this way:
private void clickRow(object sender, DataGridViewCellEventArgs e)
{
richTextBox1.Text = "There is a clickRow event with row index " + e.RowIndex;
DataGridViewRow row = adsDataGridView.Rows[e.RowIndex] as DataGridViewRow;
Ad currentAd = row.DataBoundItem as Ad;
if (currentAd != null)
{
MessageBox.Show( currentAd.ToString() );
}
}