I'm trying to search employees by Name.
Of course, there can be same Names for different employees.
I want my function to highlight all the employees with the Name i entered.
If i use break; i will get only the first value found, if I don't use break; I get all values but I also get the Error: Object reference not set to an instance of object.
Here is the full code:
// SAMEKLET DARBINIEKU
private void btn_mekletDarbinieku_Click(object sender, EventArgs e)
{
string searchValue = txt_mekletDarbinieku.Text;
DGV_darbinieks.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
try
{
foreach (DataGridViewRow row in DGV_darbinieks.Rows)
{
if (row.Cells[1].Value.ToString().Equals(searchValue))
{
row.Selected = true;
//break; // šito varētu izmantot personas kodam, jo tāds tik viens būtu
}
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
Related
could somebody help me in C# programing in an web application. Namely, how can we find out RecordUniqueId of the filtered row....in order to further edit (i.e., set values in some other columns in the same row), based on RecordUniqueId value? Or, is it possible to, upon clicking on enter button in order to search for certain row, to immediately have some actions performed on the values in some other columns of filtered row? Let me say in other words, to have some kind of automation of editing values in some other columns of the filtered row?
Thanks in advance
public class PartyVuk2010GENTableControlRow : BasePartyVuk2010GENTableControlRow
{
}
public class PartyVuk2010GENTableControl : BasePartyVuk2010GENTableControl
{
public override void MarkDiscontinued_Click(object sender, ImageClickEventArgs args)
{
try {
DbUtils.StartTransaction();
foreach (PartyVuk2010GENTableControlRow rc in this.GetSelectedRecordControls()) {
PartyVuk2010GENRecord rec;
rec = PartyVuk2010GENTable.GetRecord(rc.RecordUniqueId, true);
if (rec != null) {
rec.Discontinued = true;
rec.Test = "10";
rec.BallotOrder = PartyVuk2010GENTable.Instance.GetLargest()+1;
rec.Save();
}
}
DbUtils.CommitTransaction();
}
catch (Exception ex) {
BaseClasses.Utils.MiscUtils.RegisterJScriptAlert(this, "BUTTON_CLICK_MESSAGE", ex.Message);
}
finally {
DbUtils.EndTransaction();
}
this.DataChanged = true;
}
So if you push the enter button, and the user enters a number i want the program to throw out a exception that no integers may be entered in the text block.
private void btnEnter_Click(object sender, RoutedEventArgs e)
{
try
{
string[] words = { txtInfo.Text };
foreach (string f in words)
{
lstResults.Items.Add(f);
}
txtInfo.Text = "";
}
catch (Exception ex)
{
}
}
Very small project for school but would like to specify the exception.
the best way to do this is to implement Guard Clauses. Guard Clause is just a simple checking of the input parameters at the top of your method.
I would totally remove try catch from your code and just check if the text in the box is an integer.
private void btnEnter_Click(object sender, RoutedEventArgs e)
{
//this will check if the text from the box can be parsed as an integer then
//exit this method but show message box with your message.
if(int.TryParse(txtInfo.Text, out var test)
{
MessageBox.Show("Integers are not allowed");
return;
}
string[] words = { txtInfo.Text };
foreach (string f in words)
{
lstResults.Items.Add(f);
}
txtInfo.Text = "";
}
I hope this helps.
I have a line of code in my application which is checking for the column, it is working well for English and Japanese language but return wrong results when the language is Khmer.
The column is not present in the Table but it returns true.
The string for which it fails is as follow and sample code is as follow:
"លេខគ្រឿងបន្លាស់"
In my sample WinForms application There is one datagridview control and one button on button click following events occur.
private void btnAddColumns_Click(object sender, EventArgs e)
{
string[] arrColNames = { "លេខ", "លេខគ្រឿងបន្លាស់", "ឈ្មោះគ្រឿងបន្លាស់(THA)", "ឈ្មោះគ្រឿងបន្លាស់(ENG)", "បរិមាណ កំណត់ហេតុ/ SB", "រក្សាទុកលេខ កំណត់សម្គាល់"};
DataSet dSet = new DataSet();
dSet.Tables.Add();
try
{
for (int i = 0; i < arrColNames.Length; i++)
{
if( dSet.Tables[0].Columns.Contains(arrColNames[i]) == false)
{
dSet.Tables[0].Columns.Add(arrColNames[i]);
}
}
dataGridView1.DataSource = dSet.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
I have a DataGridView in winform with 2 combo box columns: 1) Companies, 2) Accounts.
I want to update the accounts combo box according to the selected company.
I have this code:
void recipientsDataGrid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
try
{
ComboBox cb = e.Control as ComboBox;
if (cb != null)
{
cb.SelectedValueChanged -= new EventHandler(companyCombobox_SelectedValueChanged);
cb.SelectedValueChanged += new EventHandler(companyCombobox_SelectedValueChanged);
}
}
catch (Exception ex)
{
}
}
private void companyCombobox_SelectedValueChanged(object sender, EventArgs e)
{
try
{
var currentCell = recipientsDataGrid.CurrentCellAddress;
if (currentCell.X == 3)
{
var sendingCB = sender as DataGridViewComboBoxEditingControl;
int companyId = sendingCB.SelectedValue.ToInt();
DataTable dtAccounts = m_CustomersFunctions.GetCompanyAccounts(companyId);
DataGridViewComboBoxCell cboAccounts = (DataGridViewComboBoxCell)recipientsDataGrid.Rows[currentCell.Y].Cells["Account"];
cboAccounts.ValueMember = "account_id";
cboAccounts.DisplayMember = "AccountName";
cboAccounts.DataSource = dtAccounts;
int defaultAccountId = (from row in dtAccounts.AsEnumerable()
where row.Field<string>("AccountName").EndsWith("*")
select row.Field<int>("account_id")).FirstOrDefault();
if (defaultAccountId > 0)
cboAccounts.Value = defaultAccountId;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
It works fine in the first time I select a company, but when I change the company and try to update the data source for the accounts combo box I'm getting an error:
I tried to add the items manualy and not with a DataSource, and I got the same error.
How can I fix it ?
please...
Try to clear the value of accounts cell before rebind it, like this:
private void companyCombobox_SelectedValueChanged(object sender, EventArgs e)
{
try
{
var currentCell = recipientsDataGrid.CurrentCellAddress;
if (currentCell.X == 3)
{
var sendingCB = sender as DataGridViewComboBoxEditingControl;
int companyId = sendingCB.SelectedValue.ToInt();
DataTable dtAccounts = m_CustomersFunctions.GetCompanyAccounts(companyId);
DataGridViewComboBoxCell cboAccounts = (DataGridViewComboBoxCell)recipientsDataGrid.Rows[currentCell.Y].Cells["Account"];
cboAccounts.Value = null; //Add this code
cboAccounts.ValueMember = "account_id";
cboAccounts.DisplayMember = "AccountName";
cboAccounts.DataSource = dtAccounts;
int defaultAccountId = (from row in dtAccounts.AsEnumerable()
where row.Field<string>("AccountName").EndsWith("*")
select row.Field<int>("account_id")).FirstOrDefault();
if (defaultAccountId > 0)
cboAccounts.Value = defaultAccountId;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
I had the same problem in vb.net .
Not sure if my solution is good, but it works for me.
Briefly: I create a new temporary "DataGridViewComboBoxCell" object, I assign the "DataSource" and the "Value" (which must belong to "DataSource" !!! otherwise the error "value is not valid" comes out) and assign the new object to correct cell in my "DataGridView" main object, like this:
Dim oDataGridViewComboBox As New DataGridViewComboBox With {
.DataSource = MyDataSource
.Value = MyCorrectValueContainedInDataSource
}
oMyDataGridView.Rows(iLineOfComboBox).Cells("ComboBoxColumnName") = oDataGridViewComboBox
The error "value is not valid" is thrown because the ComboBox value is not included among those present in "DataSource".
P.S.: Sorry, I don't know C# ... but the problem is the same, and I have found this post during a search of solution.
Using Entity Framework, I have a Combobox showing a list of data retrieved from a database.
using System; //I removed the other using statements here to preserve space
namespace ExTea_BackEnd
{
public partial class frmAddBreakdown : Form
{
ExTeaEntities Breakdowns;
Breakdown_Type BreakdownTypes;
public frmAddBreakdown()
{
InitializeComponent();
}
private void cmbBreakdownType_SelectedIndexChanged(object sender, EventArgs e)
{
Breakdown_Type breakdownType = (Breakdown_Type)cmbBreakdownType.SelectedItem;
string selectedBreakdownTypeId = breakdownType.BrkdwnId;
IQueryable<Breakdown_Type> breakdownTypeQuery = from t in Breakdowns.Breakdown_Types
where t.BrkdwnId == selectedBreakdownTypeId
select t;
List<Breakdown_Type> selectedBreakdownId = breakdownTypeQuery.ToList();
if (selectedBreakdownId != null && selectedBreakdownId.Count > 0)
{
BreakdownTypes = selectedBreakdownId.First();
txtBreakdownId.Text = BreakdownTypes.BrkdwnId.ToString();
}
else
{
BreakdownTypes = null;
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
try
{
Breakdown newBreakdown = new Breakdown();
Breakdown_Type breakdownType = (Breakdown_Type)cmbBreakdownType.SelectedItem;
newBreakdown.BrkdwnType = breakdownType.ToString(); //this is where the error occurs
newBreakdown.MachineId = txtMachineId.Text.Trim();
newBreakdown.MachineType = txtMachineType.Text.Trim();
newBreakdown.ReportedDate = dtpDate.Value;
newBreakdown.JobStatus = "I";
Breakdowns.AddToBreakdowns(newBreakdown);
int rowsAffected = Breakdowns.SaveChanges();
if (rowsAffected > 0)
{
MessageBox.Show(rowsAffected + " records added!", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show("Error occured! " + ex.Message);
}
}
}
}
I'm trying to save the data in the form back to the database, an error occurs when trying to cast the value selected from the Combobox. Even when I've casted it to the right type, it does not save the selected value! But this,
I'm clueless on what I'm doing wrong here? Can anyone please tell me how to correct this?
Thank you.
newBreakdown.BrkdwnType = breakdownType.ToString();
Here you are just calling .ToString() Method of your object, so it is returning the Type Name, which you are able to view in database table record, for getting the BrkDwnType Property value you should change statement to
newBreakdown.BrkdwnType = breakdownType.BrkdwnType;
it should just be:
newBreakdown.BrkdwnType = cmbBreakdownType.SelectedItem.ToString();
no need for casting selectedItem if you just want the string value. From a design point of view, you should probably normalize your database and use an ID for breakDownType that references another table with BreakDown types.