Confused with telerik event handlers - c#

I'm not sure if this is right for getting a value of a multi combo box
when user selects something i need to get it's first column value and pass it to my DB
i'm trying to get a value from the telerik Multicombo box and idon't know wich event handler to use?
here's my code
private void radMultiColumnComboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
if (radMultiColumnComboBox3.MultiColumnComboBoxElement.Rows.Count > 0)
{
radMultiColumnComboBox3.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
var item = radMultiColumnComboBox3.MultiColumnComboBoxElement.Rows[0];
radGridView1.Rows.Add(item);
}
else
{
MessageBox.Show("PLS select a row", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

This is how you get the value of the first column when the user selects a row in the Telerik's MultiColumnComboBox:
void radMultiColumnComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var value = radMultiColumnComboBox1.EditorControl.Rows[radMultiColumnComboBox1.SelectedIndex].Cells[0].Value;
}
You can find the documentation of the SelectedIndexChanged event here.

Related

dataGridView get the value of an updated cell when I press enter

I'm a beginner with windows forms and c# in visual studio. Right now, I'm doing a software, where a dataGridView element is used. In there, there is five columns (see Picture), four of them are Read-only and one of them (Column2) can the user modify it (Input Text). The number of rows are defined by the user in an external xml file. Likewise the data in the Columns 1 and 3-5.
When the data is updated in the table, the column 2 is empty and some values should be introduced by the user. The order in which each value is introduced, it is not relevant. However, the value modified in the cell should be saved automatically in a variable in order to be compared later with the values of the column3 and column 4. For example if I give the value 1,2 in the column2 a Message box should be said value between the range otherwise value incorrect.
I was looking the right event where I can save the value into variable when I press enter. I tried making some trials with the following events
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
//var selectedcell = dataGridView1.CurrentCell.Value;
//Console.Write(selectedcell);
string msg = String.Format("Row: {0}, Column: {1}",
dataGridView1.CurrentCell.RowIndex,
dataGridView1.CurrentCell.ColumnIndex);
MessageBox.Show(msg, "Current Cell");
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
//var item = dataGridView1.Rows[e.RowIndex].Cells[1].Value;
//MessageBox.Show(item.)
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
// var row = dataGridView1.Rows[e.RowIndex];
// var changedValue = (string)row.Cells[e.ColumnIndex].Value;
// Console.Write(changedValue);
}
Unfortunatelly, nothing works right now. I don't have any ideas how to implement it.
Try this :
private void dataGridView1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
var column2Value = dataGridView1.GetFocusedRowCellValue(dataGridView1.Columns[1]);
MessageBox.Show(column2Value.ToString())
}
}

Displaying selected items of listbox into a message box in C# Windows Form

I am able to display multiple selected items from a listbox into a text box on a button click but how can I display the same on a message box? I mean displaying first item on a messagebox is not an issue but multiple items at once is. Suggestions please...
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
Skill = checkedListBox1.SelectedItem.ToString();
if (e.NewValue == CheckState.Checked)
{
listBox1.Items.Add(Skill);
}
else
{
listBox1.Items.Remove(Skill);
}
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("You have selected following Skills : \n"+Skill, "Selected Skills",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
You do not show where Skill is defined, but presumably as a property of the class. You only initialize Skill in checkedListBox1_ItemCheck. If the selection has changed, that value will be stale (it will not reflect reality).
The shortest change to your code would be to not use Skill in your button handler, but rather grab the current state from the listbox (possibly put it in a local variable if you prefer that style).
private void button1_Click(object sender, EventArgs e)
{
var selectedSkills = checkedListBox1.SelectedItem.ToString();
MessageBox.Show("You have selected following Skills : \n"+selectedSkills, "Selected Skills",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
It looks like you are overwriting Skill with the last value to be checked. So, I would expect the message box to always show the Skill related to the last item that you clicked on. So, if you want to display all of them, you'll need to replace Skill in the MessageBox.Show call with something like listBox1.Items.Cast<string>().Aggregate((o, n) => o.ToString() + "," + n.ToString())
*Note: replace Cast<string> with whatever type of object Skill is.
Such as:
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
Skill = checkedListBox1.SelectedItem.ToString();
if (e.NewValue == CheckState.Checked)
{
listBox1.Items.Add(Skill);
}
else
{
listBox1.Items.Remove(Skill);
}
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("You have selected following Skills : \n"+ listBox1.Items.Cast<string>().Aggregate((o, n) => o.ToString() + "," + n.ToString()), "Selected Skills",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
You must be looping through the selected items and appending them to your textbox. In order to show on message box you need to concatenate the selected items to a string variable and use it for your message.
private void button1_Click(object sender, EventArgs e)
{
StringBuilder skills = new StringBuilder();
foreach (object item in listBox1.SelectedItems)
{
skills.AppendLine(item.ToString());
}
MessageBox.Show("You have selected following Skills : \n" + skills.ToString(), "Selected Skills",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}

How can I retrieve the previous value of a DataGridView cell using the CellValueChanged event?

I am writing a C# application that uses a DataGridView and I would like to validate the input each time a user changes the data that's there.
I began by using the CellValidating event which has a really nice CancelEdit() method that will return the cell to its previous value. However, this event is fired every time the user leaves the cell, regardless of whether or not it has changed.
Does CellValueChanged support a sort of cancel or rollback method to the previous value? This way I would be able to still validate the data, but not waste time with cells that didn't need it, but would prefer not to sacrifice the ability to restore the cell if data is invalid.
Here is a bit of code:
private void dataGrid1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if(dataGrid1.Columns[e.ColumnIndex].Name == "dateColumn")
{
String input = e.FormattedValue.ToString();
// Split the date from MM/DD/YYYY format
String[] temps = input.Split('/');
try
{
if(temps[2].Length != 4)
MessageBox.Show("The year entered is not the correct length.", "Invalid Year", MessageBoxButtons.OK);
DateTime date = new DateTime(Convert.ToInt32(temps[2]), Convert.ToInt32(temps[0]), Convert.ToInt32(temps[1]));
}
catch (Exception ex) // If exception is thrown, date was invalid
{
MessageBox.Show("The date entered was invalid.", "Invalid date", MessageBoxButtons.OK);
dataGrid1.CancelEdit(); // Set cell value back to what it was prior to user's change
e.Cancel = true; // Have focus stays with this cell rather than move down a row.
}
}
}
You could try another aproach like this:
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
newvalue = (int)dataGridView1[e.ColumnIndex, e.RowIndex].Value;
}
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
oldvalue = (int)dataGridView1[e.ColumnIndex, e.RowIndex].Value;
}
Assuming its an int,if its another datatype then will also work(except offcourse the variables oldvalue and newvalue must be that type also).
Or by your question,its just about the old value,then you will only need the CellBeginEdit event and then use the oldvalue variable inside the validating event.
IMHO better solution, cause underlying DataTable will not be marked as changed if you reject the value. Old value will be displayed automatically
private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
var underlyingDataRow = ((DataRowView)dataGridView.Rows[e.RowIndex].DataBoundItem).Row;
if (DoesNotMeetYourCondition)
{
row.RejectChanges();
}

How can I trigger dropdown while viewing asp.net page

There are two Dropdown in the asp.net page.
When I select first one (city), the second one (district) is triggered.
I have a save button for the page.
When I click the save button, I write the SelectedValues of dropdowns in to the database.
And when I open the page again, I assign the values to the dropdown's selected values.
The first one is ok when I assign dropdown.SelectedValue = "5" but the second one is not triggered. How can I trigger it?
Thanx.
I have done the same thing. Suppose you have drop down say ddlCountry and ddlCity. You need to load all cities just for the selected country.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
LoadCountriesInDropDown(ddlCountry);
ddlCountry.SelectedValue = "5" //For eg:
LoadCitiesByCountrySelected(ddlCity, ddlCountry.SelectedValue); // selected country value was set here as 5
ddlCountry_SelectedIndexChanged(null, null);
}
}
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
if(ddlCountry.SelectedItem.Text == "Select")
{
ddlCity.Items.Clear();
}
else
{
LoadCitiesByCountrySelected(ddlCity, ddlCountry.SelectedValue);
}
}
Hope this was what you wanted.

Command Argument in Runtime created imagebutton doesn't work?

Hello i am creating new image button something like this ;
e.row.cells[0].text= string.empty; //clearing the cells
Imagebutton img1= new ImageButton();
img1.Id="imgInput";
img1.ImageUrl="~/sample.jpg";
img1.CommandArgument= varID; // fetching the ID
img1.click+= new ImageClickEventHandler(imgInput_Click);
e.row.controls.add(img1)
I have written the above code in gridview rowdatabound event which.
Now in the click event (imgButton_Click; handled in second last line) i wish to put the command argument value in session;
protected void imgInput_Click(object sender, EventArgs e)
{
Session["idvalue"]= imgInput.CommandArgument;
}
But the varID here is coming as null. Why so? What am i missing here?
Please guide! Thanks
You need to use the ImageButton.Command event.
Example:
img1.Command += ImageButton_OnCommand;
protected void ImageButton_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "Sort" && e.CommandArgument == "Ascending")
Label1.Text = "You clicked the Sort Ascending Button";
else
Label1.Text = "You clicked the Sort Descending Button";
}

Categories