I am having an issue trying to match data table column names with a string containing a wildcard.
I have a data table with various column names which includes a set named like follows: "PsA", "PsB", "PsC", ect. I want to iterate through all the columns containing Ps in the title and use those titles to extract the data.
I currently have the following code which fails to return any matches. I have substituted a straight value in the if statement ("PsA") as a test, which works fine; however, when I use the wildcard, I get no matches. I have also tried Regex with no luck.
private void dfaSection_SelectedIndexChanged(object sender, EventArgs e)
{
string psText = null;
string colID = "Ps*";
offBox.Text = ""; //Clear textbox if a reselection occurs
psBox.Text = ""; //Clear textbox if a reselection occurs
info.offTitle = dfaSection.Text; //Set textbox from variable
dt = opr.findOffByTitle(info); //Get datatable from SQL database
if(dt.Rows.Count > 0)
{
offBox.Text = dt.Rows[0][5].ToString(); //Set textbox from datatable
foreach(DataColumn dc in dt.Columns) //Loop through datatable columns
{
if (dc.ColumnName.ToString() == colID) //Check that column title matches test string - Later addition--> && dt.Rows[0][dc].ToString() != null)
{
psText = dt.Rows[0][dc].ToString() + "\n\n"; //Add data from matched column to string variable
}
}
psBox.Text = psText; //Set textbox from variable
}
}
Edit: Issue fixed using .Contains(colID) Column names now being matched, string are now not being loaded to the psText variable, but I'll spend some time with that and get it working. Thanks Skaros Ilias.
I am not so failiar with C, but == for sure is not the right method.
you need to use the contains method. As I said, I am not familiar with it, but the docs have a good example of it.
String s = "This is a string.";
String sub1 = "this";
Console.WriteLine("Does '{0}' contain '{1}'?", s, sub1);
StringComparison comp = StringComparison.Ordinal;
Console.WriteLine(" {0:G}: {1}", comp, s.Contains(sub1, comp));
take a breakpoint a check what is column name and colID at failure moment.How are you going to solve problems, when don't even know values?
Use dc.ColumnName.ToString().StartsWith(). Contains() is good only untill string endswith.
As per this answer before : Regular expression wildcard
And here is sample of code :
private static bool Match(string pattern, string stringToCheck) {
var finalPattern = pattern.Replace("*", ".*?");
Regex regex = new Regex(finalPattern);
return regex.IsMatch(stringToCheck);
}
Related
I'm looping through a large excel file full of Products. We want to only process rows where Brand = x, or Product = 'y'. The following code worked there was a predictable filter ie. Product1, however we are unsure what data the xsl file will hold and it could be something like "Product1 (buy me)" which wouldn't pass with this logic and the record would get ignored.
What technique can we use to match Product names with our filters? Regex, pattern matching etc. ? Or do I simply need to split the filter and loop through each? Seems like there shuold be a more elegant way.
private static bool SkipRecord(string strFilters, string key, DataRow row)
{
//include the record if it matches our filter
var strField = row[key].ToString();
bool skip = true;
if (strField != null && strField != "")
{
skip = !strFilters.ToLower().Contains(strField.ToLower());
}
return skip;
}
List<ResultRow> xlsRows = new List<ResultRow>();
foreach (DataRow row in dataTable.Rows)
{
if (SkipRecord(f.brandFlag, "Brand", row) && SkipRecord(f.productFlag, "Name", row))
continue;
}
appSettings.json
"CustomSettings": {
"BrandFlag": "Gibson|Fender|Jackson",
"ProductFlag": "Product 1|ProductTwo|Product3",
}
(One comment first, I think the first method is missing the
return skip;
line).
For the specific case you mentioned it would work if you changed the order of the "Contains" arguments:
skip = !strField.ToLower().Contains(strFilters.ToLower());
Excel column name may contain trailing spaces which would hit exception if the column don't match due to spaces.
I am finding ways to handle trailing spaces in the column name in datatable.
foreach (DataRow row in caseTable.Rows)
{
foreach (DataColumn column in caseTable.Columns)
{
if (!(string.isNullOrEmpty(column.toString())))
{
//Cannot assign value to 'column' because it is in a 'foreach iteration variable'
column = column.ToString().TrimStart().TrimEnd();
trimmed = 1;
}
}
while (trimmed == 0) ;
}
...
//errored out due to 'Excel.firstName' value not existing in DataTable due to trailing spaces
if (row[Excel.firstName].ToString().Trim() != "")
{
caseEntity.Attributes[Case.firstName] = row[Excel.firstName];
}
There are two mistakes in your code:
You are iterating a sequence with foreach and trying to modify the iterated items; thus, the exception that is throwing.
You are trying to replace the column object instead of its name.
Additionally, you are abusing ToString and, as already stated by Michal Turczyn, you are not using built-in String methods explicitly designed to test for empty or null strings.
You can try replacing the code inside the inner loop with
var oldName = column.ColumnName;
if (!string.IsNullOrEmpty(oldName))
{
var newName = oldName.Trim();
if (newName != oldName)
{
column.ColumnName = newName;
trimmed = 1;
}
}
From this replacement code You can see that:
You should read/write ColumnName property instead of using ToString.
You can avoid invoking TrimStart and TrimEnd one after another (in any order) by using Trim.
You should use string.IsNullOrEmpty instead of checking for null andempty string.
By declaring oldName and newName, you can track a column name as trimmed only when that's really true.
More over, if you are assigning only values 0 and 1 to trimmed variable, then you should consider declaring it as bool and assign false or true.
Otherwise you can keep trimmed variable as a number, but increasing its value rather than assigning always the same constant (1).
If what you are trying to change is not the column name, but is the cell value in the current row for such column, then you are missing to get (and later set) the cell value (you are evaluating and trying to change only the column name).
In that case, the inner loop code become:
var oldValue = row[column] as string;
if (!string.IsNullOrEmpty(oldValue))
{
var newValue = oldValue.Trim();
if (newValue != oldValue)
{
row[column] = newValue;
trimmed = 1;
}
}
I have a combo-box that contains lots of entries like this small extract
1R09ST75057
1R11ST75070
1R15ST75086
1R23ST75090
2R05HS75063
2R05ST75063
3R05ST75086
2R07HS75086
The user now enters some information in the form that result in a string being produced that has a wildcat (unknown) character in it at the second character position
3?05ST75086
I now want to take this string and search\filter through the combo-box list and be left with this item as selected or a small set of strings.
If I know the string without the wildcat I can use the following to select it in the Combo-box.
cmbobx_axrs75.SelectedIndex = cmbobx_axrs75.Items.IndexOf("2R05HS75063");
I thought I could first create a small subset that all have the first char the same then make a substring of each minus the first two chars and check this but I can have a large amount of entries and this will take too much time there must be an easier way?
Any ideas how I can do this with the wildcat in the string please?
Added info:
I want to end up with the selected item in the Combobox matching my string.
I choose from items on the form and result in string 3?05ST75086. I now want to take this and search to find which one it is and select it. So from list below
1R05ST75086
2R05ST75086
3R05ST75086
6R05ST75086
3R05GT75086
3R05ST75186
I would end up with selected item in Combo-box as
3R05ST75086
You could use regular expressions. Something like this:
string[] data = new string[]
{
"1R09ST75057",
"1R11ST75070",
"1R15ST75086",
"1R23ST75090",
"2R05HS75063",
"2R05ST75063",
"3R05ST75086",
"2R07HS75086"
};
string pattern = "3*05ST75086";
string[] results = data
.Where(x => System.Text.RegularExpressions.Regex.IsMatch(x, pattern))
.ToArray();
You can use a regular expression for this task. First, you need a method to convert your pattern string to Regex like this (it should handle "*" and "?" wildcards):
private static string ConvertWildCardToRegex(string value)
{
return "^" + Regex.Escape(value).Replace("\\?", ".").Replace("\\*", ".*") + "$";
}
Then you will use it like the following:
List<string> comboBoxValues = new List<string>()
{
"1R09ST75057",
"1R11ST75070",
"1R15ST75086",
"1R23ST75090",
"2R05HS75063",
"2R05ST75063",
"3R05ST75086",
"2R07HS75086"
};
string searchPattern = "3?05ST75086";
string patternAsRegex = ConvertWildCardToRegex(searchPattern);
var selected = comboBoxValues.FirstOrDefault(c => Regex.IsMatch(c, patternAsRegex));
if (selected != null)
{
int selectedIndex = comboBoxValues.IndexOf(selected);
}
This assumes you only care about first found match. If you need all matches then substitute FirstOrDefault(...) with Where(...) clause and swap "if" statement with a foreach loop.
Thanks to all that helped I used a combination of items from all answers so everyone helped me answer this.
I added this function from the answers as it seems a good idea, thanks
private static string ConvertWildCardToRegex(string value)
{
return "^" + Regex.Escape(value).Replace("\\?", ".").Replace("\\*", ".*") + "$";
}
Then I get the combo box items into a list. I search the list and make some more decisions based on the result of the search.
List<string> comboBoxValues = new List<string>();
for (int i = 0; i < cmbobx_in_focus.Items.Count; i++)
{
comboBoxValues.Add(cmbobx_in_focus.GetItemText(cmbobx_in_focus.Items[i]));
}
string[] results = comboBoxValues
.Where(x => Regex.IsMatch(x, ConvertWildCardToRegex(lbl_raster_used.Text)))
.ToArray();
I now have array called results which is easy to work with.
I have a combobox and a datatable.
I've added all of the elements of one column in the datatable to the combobox items.
Now whenever the user chooses a item in the combobox, I want to go to the datatable and compare the column, if there's a match, it will do some code.
I have the following
private void comboBox8_SelectedIndexChanged(object sender, EventArgs e)
{
string str = comboBox8.SelectedItem.ToString();
int z = 0;
foreach (var row in datatable.Rows)
{
int i = 0; i++;
if (datatable.Rows[row]["Cidade"] == str)
{
z = i;
}
}
}
"Cidade" is the column name that matches the options in the combobox.
The Problem is that the code doesn't identify the ìf` condition as valid, saying there are invalid arguments
Edit: worked it around like this:
private void comboBox8_SelectedIndexChanged(object sender, EventArgs e)
{
string str = comboBox8.SelectedItem.ToString();
int z = 0;
for (int i = 0; i < DataAccess.Instance.tabelasismica.Rows.Count; i++)
{
if (DataAccess.Instance.tabelasismica.Rows[i]["Cidade"] == str)
{
z = i;
}
}
MessageBox.Show(z.ToString());
MessageBox.Show(DataAccess.Instance.tabelasismica.Rows[z]["Cidade"].ToString());
}
Standard way of doing things like this is to use data-binding. You'd simply set your ComboBox's DataSource to your DataTable. The code would roughly look like this:
comboBox8.DataSource = datatable;
comboBox8.DisplayMember = "Cidade"
comboBox8.ValueMember = "PrimaryKeyColumnOfYourTable"
Now in the SelectedIndexChanged event, you simply use comboBox8.SelectedValue property to get the ID of the selected row. If you have strongly typed DataSet, your DataTable will have a function named FindByYourPKColumn() that you can use to find the row using this ID.
datatable.Rows[row]["Cidade"] is of type object - you need to convert it to a string before comparing it to str, like this:
if (datatable.Rows[row]["Cidade"].ToString() == str)
{ ... }
Try this in place of the for loop
foreach (DataRow row in dDataAccess.Instance.tabelasismica.Rows)
{
if (row["Cidade"].ToString() == str)
{
z = dDataAccess.Instance.tabelasismica.Rows.IndexOf(row);
}
}
or
foreach (DataRow row in dataTable.Rows)
{
if (row["Cidade"].ToString() == str)
{
z = dataTable.Rows.IndexOf(row);;
}
}
Being said that, standard practice in using ComboBoxes, ListBoxes etc with datasources is to to have a distinct column in the data-table assigned as the ValueMember of the ComboBox, which makes life even easier - as suggested by #dotNET.
comboBox8.DataSource= dataTable; //the data table which contains data
comboBox8.ValueMember = "id"; // column name which you want in SelectedValue
comboBox8.DisplayMember = "name"; // column name that you need to display as text
That way you don't have to iterate through the dataTable to find the index of the row, and you can use the ID (ValueMember) to continue process as required.
Example here
#dotNET's answer is the preferred method to solve your specific problem.
However to solve the general problem find a value in a dataset your best bets are to either
Use the ADO.NET methods Find or Select e.g.
var results = dataset.Select(string.Format("Cidade = {0}",str));
if (results.Count() != 0 )
{
...
}
Or use System.Data.DataSetExtensions
if (datatable.AsEnumerable().Any( x=> x.Field<string>("Cidade") == str ))
{
....
}
I have a gridview that uses a DataTable as datasource. When I'm trying to hardcode the RowFilter string it works well, but when I use GetFilterExpression() it doesn't work. Why?
I've checked the string returned by GetFilterExpression and match it with an if check and I saw it exactly matches my hardcoded string.
var list = (List<ResaveBase>)listGridRows;
var dt = ToDataTable(list);
dt.DefaultView.RowFilter = GetFilterExpression(); //When hardcoding this, it works
gvwResavePositions.DataSource = dt;
gvwResavePositions.DataBind();
private string GetFilterExpression()
{
string filterExpression = String.Empty;
filterExpression = string.IsNullOrEmpty(txtPaperId.Text)
? string.Empty
: string.Format("strPaperId IN ({0})", txtPaperId.Text);
return filterExpression;
}
This should fix it:
gvwResavePositions.DataSource = dt.DefaultView;
as per my knowledge expression must comes in quotation marks. it may possible your function results comes without quotes.
you can write logic in function and return value only and call function like -
String.Format("strPaperId IN ({0})", GetFilterExpression());