DataGridView RowValidation Error - c#

I'm sync my data from Grid to DataBase using really weird way :
for example :
#region Line methods
private void LinesView_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
{
lineTableAdapter.Update(fRIIBDataSet.Line);
}
private void LinesView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
lineTableAdapter.Update(fRIIBDataSet.Line);
}
private void LinesView_RowValidated(object sender, DataGridViewCellEventArgs e)
{
lineTableAdapter.Update(fRIIBDataSet.Line);
}
#endregion
but when I switched some columns to ComboBox I need to make some trick before Update alike that :
private void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e)
{
//deltaTableAdapter.Update(fRIIBDataSet.Delta); TODO
}
private void dataGridView1_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
{
deltaTableAdapter.Update(fRIIBDataSet.Delta);
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (lambdacat != null)
{
string selected = (LimView.Rows[e.RowIndex].Cells[1] as DataGridViewComboBoxCell).FormattedValue.ToString();
if (selected != "")
{
int find = Array.IndexOf(dict, dict.Where(x => x == selected).FirstOrDefault());
LimView.Rows[e.RowIndex].Cells[0].Value = dictiddarray[find];
//deltaTableAdapter.Update(fRIIBDataSet.Delta);
}
}
//deltaTableAdapter.Update(fRIIBDataSet.Delta);
}
troubles comes after uncommenting Update method.
When I opening the window with a table I've got error message : Damaged the internal index DataTable: "5. " How can I fix / avoid this error ?

Does modifying it to this work ?
if (selected != "")
{
int find = Array.IndexOf(dict, dict.Where(x => x == selected).FirstOrDefault());
LimView.Rows[e.RowIndex].Cells[0].Value = dictiddarray[find];
fRIIBDataSet.Delta.BeginInit(); //
deltaTableAdapter.Update(fRIIBDataSet.Delta);
fRIIBDataSet.Delta.EndInit();
}

Related

if statements in C# for a textbox to change back to normal when i press a button for the second time

This is the code that i used to change the text in the text box from "Livre" to "Ocupado"
What code should i use to change it from "Ocupado" to "Livre"
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text="Ocupado";
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "Livre")
{
textBox1.Text = "Ocupado";
}
else
{
textBox1.Text = "Livre";
}
}
you can add a variable to your class
e.g.:
bool livre = true;
private void button1_Click(object sender, EventArgs e)
{
if (livre)
{
textBox1.Text="Ocupado";
}
else
{
textBox1.Text="Livre";
}
livre = !livre;
}

Xamarin - Morse code app usuing the flaslight

I am trying to make a morse code for a college project, what I'm trying to do is use a 2 dimensional array to save the morse code people input to a text file and then be able to load it from the text file, my logic was to was that within the array was this array[morse name][morse input]. what I need to figure out first is how to send data from methods / buttons OBtn_Clicked , LBtn_Clicked, SBtn_Clicked and EndBtn_Clicked to NewMorseBtn_Clicked to add into the array which will then write it out to a text file I've created.
namespace FlashLightApp2018
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MorsePage : ContentPage
{
//bool exitLoop = false;
public MorsePage()
{
InitializeComponent();
}
private async void NewMorseBtn_Clicked(object sender, EventArgs e)
{
bool isTextEmpty = String.IsNullOrEmpty(MorseName.Text);
if (isTextEmpty)
{
}
else
{
OBtn.IsEnabled = true;
LBtn.IsEnabled = true;
SBtn.IsEnabled = true;
EndBtn.IsEnabled = true;
// String morseName = MorseName.Text;
//String[,] morseSave = new String[100,100];
}
//File.WriteAllText(morseName, text);
//while (exitLoop != true)
//{
//}
}
private void LoadMorseBtn_Clicked(object sender, EventArgs e)
{
}
private void PlayMorseBtn_Clicked(object sender, EventArgs e)
{
}
private void OBtn_Clicked(object sender, EventArgs e)
{
}
private void LBtn_Clicked(object sender, EventArgs e)
{
}
private void SBtn_Clicked(object sender, EventArgs e)
{
}
private void EndBtn_Clicked(object sender, EventArgs e)
{
}
}
}
first, declare you data at the class level (outside of a single method) so that it is accessible from throughout your class
string morseData = string.Empty;
then have your different button methods update the data
private void OBtn_Clicked(object sender, EventArgs e)
{
morseData += ".";
}
private void LBtn_Clicked(object sender, EventArgs e)
{
moreseData += "-";
}

How to disable a combobox on clicking on another combox list data in C sharp?

I want to design a windows form using C sharp on Visual Studio 2013.
I go through the Source from here. but did not got it properly.
for that I have 3 combobox. I want to disable combobox2 when I click on combobox1 NSSCM element and enable when click on NSSFO element.
Below is my part of code snippet:
namespace NSE_First_Form
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MaximizeBox = false;
MinimizeBox = false;
if (true)
{
comboBox1.Items.Add(Exchange.NSSCM.ToString());
comboBox1.Items.Add(Exchange.NSSFO.ToString());
comboBox1.Items.Add(Exchange.BSSCM.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
string selectedItem = string.Empty;
ProcessValue(selectedItem);
}
public enum Exchange
{
NSSCM = 1,
NSSFO = 2,
BSSCM = 3
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
Try this:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == 0)
comboBox2.Enabled = false;
if (comboBox1.selectedIndex == 1)
comboBox2.Enabled = true;
}
Try this:
//This will disable combobox2 on the click of it
private void comboBox1_Click(object sender, EventArgs e)
{
comboBox2.Enabled = false;
}
//This will enable combobox2 on the click of it
private void comboBox1_Click(object sender, EventArgs e)
{
comboBox2.Enabled = true;
}
Because you want it on click, use the CLICK event, instead of SelectedIndexChange event.

Can't put more than one number in the textbox. in my Simple Calculator program

Need help in making a simple calculator. i can't put more than one number in my calculator's textbox. Everytime i put a second number it replaces the first one need help!
I can't exceed more than one input number in my Calculator's Textbox instead it replaces the first number with a second number input
namespace Calculator_Project
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void InputOutputArea_TextChanged(object sender, EventArgs e)
{
}
private void One_Click(object sender, EventArgs e)
{
int Input = 1;
InputOutputArea.Text = Input.ToString();
}
private void Two_Click(object sender, EventArgs e)
{
int Input = 2;
InputOutputArea.Text = Input.ToString();
}
private void Three_Click(object sender, EventArgs e)
{
}
private void Four_Click(object sender, EventArgs e)
{
}
private void Five_Click(object sender, EventArgs e)
{
}
private void Six_Click(object sender, EventArgs e)
{
}
private void Seven_Click(object sender, EventArgs e)
{
}
private void Eight_Click(object sender, EventArgs e)
{
}
private void Nine_Click(object sender, EventArgs e)
{
}
private void Eql_Click(object sender, EventArgs e)
{
}
private void AddB_Click(object sender, EventArgs e)
{
}
private void Minus_Click(object sender, EventArgs e)
{
}
private void MultiplyB_Click(object sender, EventArgs e)
{
}
private void DivideB_Click(object sender, EventArgs e)
{
}
private void Zero_Click(object sender, EventArgs e)
{
}
private void ResetB_Click(object sender, EventArgs e)
{
InputOutputArea.Clear();
}
}
}
You should use
InputOutputArea.Text += Input.ToString();
(note the '+') in order to append to a text box.
private void Two_Click(object sender, EventArgs e)
{
int Input = 2;
InputOutputArea.Text += Input.ToString();
}
You must use += to add other text to next of first text
Here is your problem:
InputOutputArea.Text = Input.ToString();
This replaces the content of the textbox instead of adding to it.
InputOutputArea.Text += Input.ToString();
the above code should do as you ask.
Good to remember is that concatenating strings with + is rather inefficient, so don't do this in performance critical code unless absolutely necessary. In those cases a String-builder is almost always better.
Every answers talking about the Concatenation of the previous text with the current, But I would like to suggest something more than that;
You need not to create separate event handlers for all your buttons that are doing same tasks, Hope that the Text of each button will be the number that you need to display in the textBox(say btnOne will holds 1 and btnTwoholds 2 and so on). By make use of this Text we can reuse the handlers like the following, Let btnNumber_Click be the handler and which is defined like the following:
private void btnNumber_Click(object sender, EventArgs e)
{
Button currentButton = sender as Button;
InputOutputArea.Text += currentButton.Text;
}

Combobox not showing Items while selecting the parameter from Listbox c#

I want my ComboBox to show a set of parameters every time I select something from the ListBox, but it is not showing anything inside the ComboBox.
This is what I have so far...
private void Form1_Load(object sender, EventArgs e)
{
listBox4.Items.Add("BE");
listBox4.Items.Add("MBA");
listBox4.Items.Add("Pharmacy");
}
private void listBox4_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if ((string)listBox4.SelectedItem == "BE")
{
comboBox1.Items.Add("CSE");
comboBox1.Items.Add("IT");
comboBox1.Items.Add("ME");
comboBox1.Items.Add("EX");
comboBox1.Items.Add("CE");
}
if ((string)listBox4.SelectedItem == "Pharmacy")
{
comboBox1.Items.Add("Pharmaceutical Chemistry");
comboBox1.Items.Add("Pharmacology");
}
if ((string)listBox4.SelectedItem == "MBA")
{
comboBox1.Items.Add("Retail Management");
comboBox1.Items.Add("HR");
}
}
Here's the output
You've placed your code in the wrong event.
// This is where your code belongs.
private void listBox4_SelectedIndexChanged(object sender, EventArgs e)
{
if ((string)listBox4.SelectedItem == "BE")
{
comboBox1.Items.Add("CSE");
comboBox1.Items.Add("IT");
comboBox1.Items.Add("ME");
comboBox1.Items.Add("EX");
comboBox1.Items.Add("CE");
}
if ((string)listBox4.SelectedItem == "Pharmacy")
{
comboBox1.Items.Add("Pharmaceutical Chemistry");
comboBox1.Items.Add("Pharmacology");
}
if ((string)listBox4.SelectedItem == "MBA")
{
comboBox1.Items.Add("Retail Management");
comboBox1.Items.Add("HR");
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// THIS WAS THE WRONG PLACE
}
You should give your controls meaningful names so it's easier to determine what is coming from where.
Whenever populating the ComboBox, you should clear it first.
Most importantly, you are checking for the SelectedIndexChanged on the ComboBox instead of the ListBox. What happens if you move it up?
private void Form1_Load(object sender, EventArgs e)
{
listBox4.Items.Add("BE");
listBox4.Items.Add("MBA");
listBox4.Items.Add("Pharmacy");
}
private void listBox4_SelectedIndexChanged(object sender, EventArgs e)
{
if ((string)listBox4.SelectedItem == "BE")
{
comboBox1.Items.Add("CSE");
comboBox1.Items.Add("IT");
comboBox1.Items.Add("ME");
comboBox1.Items.Add("EX");
comboBox1.Items.Add("CE");
}
if ((string)listBox4.SelectedItem == "Pharmacy")
{
comboBox1.Items.Add("Pharmaceutical Chemistry");
comboBox1.Items.Add("Pharmacology");
}
if ((string)listBox4.SelectedItem == "MBA")
{
comboBox1.Items.Add("Retail Management");
comboBox1.Items.Add("HR");
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
listBox4.Items.Add("BE");
listBox4.Items.Add("MBA");
listBox4.Items.Add("Pharmacy");
}
private void listBox4_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1.Items.Clear();
if ((string)listBox4.SelectedItem == "BE")
{
comboBox1.Items.Add("CSE");
comboBox1.Items.Add("IT");
comboBox1.Items.Add("ME");
comboBox1.Items.Add("EX");
comboBox1.Items.Add("CE");
}
if ((string)listBox4.SelectedItem == "Pharmacy")
{
comboBox1.Items.Add("Pharmaceutical Chemistry");
comboBox1.Items.Add("Pharmacology");
}
if ((string)listBox4.SelectedItem == "MBA")
{
comboBox1.Items.Add("Retail Management");
comboBox1.Items.Add("HR");
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
Well, you should update (remove old and add new) comboBox1.Items on listBox4 change:
// Please, notice "listBox4"
private void listBox4_SelectedIndexChanged(object sender, EventArgs e) {
String selected = listBox4.SelectedItem as String;
// we don't want blinking - too many re-draws
combobox1.BeginUpdate();
try {
//DONE: do not forget to remove old items
combobox1.Items.Clear();
if (selected == "BE") {
combobox1.Items.AddRange("CSE", "IT", "ME", "EX", "CE");
else if (selected == "Pharmacy") {
combobox1.Items.AddRange("Pharmaceutical Chemistry", "Pharmacology");
else if (selected == "MBA")
combobox1.Items.AddRange("Retail Management", "HR");
finally {
combobox1.EndUpdate();
}
}
And it seems that comboBox1 is of no use, at least now:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
//TODO: put here logic on comboBox1 change, e.g. on "Retail Management" selection
}

Categories

Resources