how do I put the focus back on the last cell edited? - c#

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
}
}

Related

Toolstripmenuitem not running method C#

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 don't want radio button check change C# Winform

Two radio button in may program.
names HEX and ASCII
When user checked radio button, text is chanege
void rdo_HEX_CheckedChanged(object sender, EventArgs e)
{
if (rdo_HEX.Checked)
{
try
{
textbox1.Text = AsciiToHex(textbox1.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
try
{
textbox1.Text = HexToAscii(textbox1.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
But I don't want occur checkedChange evnet When an error occurs...
if error occurs when checkedChange, just radio button check is change and text is retained.
For example,
First text is 'ABCD' and ASCII is checked and it convert to '41424344' when HEX radio button is checked.
And '4142434' convert to Ascii, error ouccur, so text is '4142434' but ASCII radio button is checked..
So user check HEX radio button '4142434' convert to '34313432343334'
I dont want this...I want not change checked radio button when error occur.
How can I do?
Just set the one checked after disabling the CheckChanged
void rdo_HEX_CheckedChanged(object sender, EventArgs e)
{
if (rdo_HEX.Checked)
{
try
{
textbox1.Text = AsciiToHex(textbox1.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
rdo_HEX.CheckedChanged -= rdo_HEX_CheckedChanged;
rdo_HEX.Checked = false;
rdo_HEX.CheckedChanged += rdo_HEX_CheckedChanged;
}
}
else
{
try
{
textbox1.Text = HexToAscii(textbox1.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
rdo_HEX.CheckedChanged -= rdo_HEX_CheckedChanged;
rdo_HEX.Checked = true;
rdo_HEX.CheckedChanged += rdo_HEX_CheckedChanged;
}
}
}
if im understanding you correctly you dont want to keep the radio-button checked in case an error is thrown. Im not seeing the implementation on both buttons here, im just seeing one of them. So you will have to replicate this for your ascii radio button.
void rdo_HEX_CheckedChanged(object sender, EventArgs e)
{
if (rdo_HEX.Checked)
{
try
{
textbox1.Text = AsciiToHex(textbox1.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
rdo_HEX.Checked = false;
}
}
else
{
try
{
textbox1.Text = HexToAscii(textbox1.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}

Access Database Not Updating or Inserting or Deleting C# using Table Adapters and DataBinding

I am trying to update my Access File Using Table Adapter .
When i add the Data my DataGridview shows that data has been added or deleted
But when I Check my access file it stays the same
THere is no Compilation Error or No Excetion Error
This is the Following Code
This is the Image of the Data Grid View
....
private void adminFlights_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'appData.Airline' table. You can move, or remove it, as needed.
this.airlineTableAdapter.Fill(this.appData.Airline);
airlineBindingSource.DataSource = this.appData.Airline;
}
private void dataGridView2_KeyDown(object sender, KeyEventArgs e)
{//TO delete data from Data Grid view
if (e.KeyCode == Keys.Delete)
{
MessageBox.Show(e.KeyCode.ToString());
if (MessageBox.Show("Are You Sure You want TO Delete ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
airlineBindingSource.RemoveCurrent();
}}}
private void btnSave_Click(object sender, EventArgs e)
{//this the Button i am using save the Data into acess using DatagridView
try
{
dataGridView2.DataSource = airlineBindingSource.DataSource;
airlineBindingSource.EndEdit();
airlineTableAdapter.Update(this.appData.Airline);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
}
}
private void Add_flights_Click(object sender, EventArgs e)
{
try
{
panel.Enabled = true;
Airline_add.Focus();
this.appData.Airline.AddAirlineRow(this.appData.Airline.NewAirlineRow());
airlineBindingSource.MoveLast();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
airlineBindingSource.ResetBindings(false);
}
}

dataGridView default error dialog handle

I am trying to hide default datagridview error dialog.
I put in the code this event handler:
this.dataGridView2.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(dataGridView2_DataError);
private void dataGridView2_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
//empty so it doesn't show anything
}
But still when i try this and leave datagridview cell empty ( delete everything from it), it show me dialog box with error.
Screenshot of error:
Try to Handle and Cancel the event:
private void dataGridView2_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
e.Cancel = true;
}
Also, subscribe to the event in InitializeComponent()
private void InitializeComponent()
{
//...
this.dataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.dataGridView2_DataError);
}
try to use this code to handle the event:
private void dataGridView2_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
e.Cancel = true;
}
Try the next code , it's work!
private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
try
{
//To handle 'ConstraintException' default error dialog (for example, unique value)
if ((e.Exception) is System.Data.ConstraintException)
{
// ErrorText glyphs show
dataGridView1.Rows[e.RowIndex].ErrorText = "must be unique value";
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "must be unique value";
//...or MessageBox show
MessageBox.Show(e.Exception.Message, "Error ConstraintException",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
//Suppress a ConstraintException
e.ThrowException = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ERROR: dataGridView1_DataError",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

Using cellcontentclick event in a c# program that activates a link

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);
}
}

Categories