I'm generating a report based on a database table. In the table I have genders saved as either 0 or 1 (male/female). In the report I would like to show the string value rather than showing the numbers. I tried messing with the code, ie.
this.ItemsTableAdapter.Fill(this.InventoryDataSet.Items);
foreach (DataRow row in this.InventoryDataSet.Items.Rows)
{
if (row["gender"].ToString() == "0")
{
row["gender"] = "Male";
}
}
this.reportViewer1.RefreshReport();
However this breaks the report complelty (no data is shown). How can I achieve what I want?
[edit]
I came a little closer to the solution (I think) with this:
this.InventoryDataSet.Items.Columns.Add("genderVerbose", typeof(string));
foreach (DataRow row in this.InventoryDataSet.Items.Rows)
{
if (row["gender"].ToString() == "0")
{
row["genderVerbose"] = "Male";
}
}
However now I get this error when trying to use the field in the report:
Error 2 The Value expression for the text box ‘color’ refers to the
field ‘genderVerbose’. Report item expressions can only refer to
fields within the current dataset scope or, if inside an aggregate,
the specified dataset scope. Letters in the names of fields must use
the correct case.
How about assigning Enum values to the cells? Enum.ToString() by default gives the value name in code as the string. So with:
public enum Gender { Male, Female }
You can just shove those into DataGridView cells and it will show that way. You can test values by
if ((Gender)row["gender"].Value == Gender.Male)
{
// do things...
}
If you need this to be user-selectable, you can use the DataGridViewComboBoxCell.
Related
UPDATE
I think I have found what is causing the issue here https://stackoverflow.com/a/5665600/19393524
I believe my issue lies with my use of .DefaultView. The post thinks when you do a sort on it it is technically a write operation to the DataTable object and might not propagate changes made properly or entirely. It is an interesting read and seems to answer my question of why passing valid data to a DataRow is throwing this exception AFTER I make changes to the datatable
UPDATE:
Let me be crystal clear. I have already solved my problem. I would just like to know why it is throwing an error. In my view the code should work and it does.. the first run through.
AFTER I have already deleted the column then added it back (run this code once)
When I debug my code line by line in Visiual studio and stop at the line:
data.Rows[i].SetField(sortColumnNames[k], value);
the row exists
the column exisits
value is not null
sortColumnNames[k] is not null and contains the correct column name
i is 0
Yet it still throws an exception. I would like to know why. What am I missing?
Sorry for the long explanation but this one needs some context unfortunately.
So my problem is this, I have code that sorts data in a DataTable object by column. The user picks the column they want to sort by and then my code sorts it.
I ran into an issue where I needed numbers to sort as numbers not strings (all data in the table is strings). eg (string sorting would result in 1000 coming before 500)
So my solution was to create a temporary column that uses the correct datatype so that numbers get sorted properly and the original string data of the number remains unchanged but is now sorted properly. This worked perfectly. I could sort string numeric data as numeric data without changing the formatting of the number or data type.
I delete the column I used to sort afterwards because I use defaultview to sort and copy data to another DataTable object.
That part all works fine the first time.
The issue is when the user needs to do a different sort on the same column. My code adds back the column. (same name) then tries to add values to the column but then I get a null reference exception "Object not set to an instance of an object"
Here is what I've tried:
I've tried using AcceptChanges() after deleting a column but this did nothing.
I've tried using column index, name, and column object returned by DataTable.Columns.Add() in the first parameter of SetField() in case it was somehow referencing the "old" column object I deleted (this is what I think the problem is more than likely)
I've tried changing the value of the .ItemArray[] directly but this does not work even the first time
Here is the code:
This is the how the column names are passed:
private void SortByColumn()
{
if (cbAscDesc.SelectedIndex != -1)//if the user has selected ASC or DESC order
{
//clears the datatable object that stores the sorted defaultview
sortedData.Clear();
//grabs column names the user has selected to sort by and copies them to a string[]
string[] lbItems = new string[lbColumnsToSortBy.Items.Count];
lbColumnsToSortBy.Items.CopyTo(lbItems, 0);
//adds temp columns to data to sort numerical strings properly
string[] itemsToSort = AddSortColumns(lbItems);
//creates parameters for defaultview sort
string columnsToSortBy = String.Join(",", itemsToSort);
string sortDirection = cbAscDesc.SelectedItem.ToString();
data.DefaultView.Sort = columnsToSortBy + " " + sortDirection;
//copies the defaultview to the sorted table object
sortedData = data.DefaultView.ToTable();
RemoveSortColumns(itemsToSort);//removes temp sorting columns
}
}
This is where the temp columns are added:
private string[] AddSortColumns(string[] items)//adds columns to data that will be used to sort
//(ensures numbers are sorted as numbers and strings are sorted as strings)
{
string[] sortColumnNames = new string[items.Length];
for (int k = 0; k < items.Length; k++)
{
int indexOfOrginialColumn = Array.IndexOf(columns, items[k]);
Type datatype = CheckDataType(indexOfOrginialColumn);
if (datatype == typeof(double))
{
sortColumnNames[k] = items[k] + "Sort";
data.Columns.Add(sortColumnNames[k], typeof(double));
for (int i = 0; i < data.Rows.Count; i++)
{
//these three lines add the values in the original column to the column used to sort formated to the proper datatype
NumberStyles styles = NumberStyles.Any;
double value = double.Parse(data.Rows[i].Field<string>(indexOfOrginialColumn), styles);
bool test = data.Columns.Contains("QtySort");
data.Rows[i].SetField(sortColumnNames[k], value);//this is line that throws a null ref exception
}
}
else
{
sortColumnNames[k] = items[k];
}
}
return sortColumnNames;
}
This is the code that deletes the columns afterward:
private void RemoveSortColumns(string[] columnsToRemove)
{
for (int i = 0; i < columnsToRemove.Length; i++)
{
if (columnsToRemove[i].Contains("Sort"))
{
sortedData.Columns.Remove(columnsToRemove[i]);
}
}
}
NOTE:
I've been able to fix the problem by just keeping the column in data and just deleting the column from sortedData as I use .Clear() on the sorted table which seems to ensure the exception is not thrown.
I would still like an answer though as to why this is throwing an exception. If I use .Contains() on the line right before the one where the exception is thrown is says the column exists and returns true and in case anyone is wondering the params sortColumnNames[k] and value are never null either.
Your problem is probably here:
private void RemoveSortColumns()
{
for (int i = 0; i < data.Columns.Count; i++)
{
if (data.Columns[i].ColumnName.Contains("Sort"))
{
data.Columns.RemoveAt(i);
sortedData.Columns.RemoveAt(i);
}
}
}
If you have 2 columns, and the first one matches the if, you will never look at the second.
This is because it will run:
i = 0
is i < columns.Count which is 2 => yes
is col[0].Contains("sort") true => yes
remove col[0]
i = 1
is i < columns.Count which is 1 => no
The solution is to readjust i after the removal
private void RemoveSortColumns()
{
for (int i = 0; i < data.Columns.Count; i++)
{
if (data.Columns[i].ColumnName.Contains("Sort"))
{
data.Columns.RemoveAt(i);
sortedData.Columns.RemoveAt(i);
i--;//removed 1 element, go back 1
}
}
}
I fixed my original issue by changing a few lines of code in my SortByColumn() method:
private void SortByColumn()
{
if (cbAscDesc.SelectedIndex != -1)//if the user has selected ASC or DESC order
{
//clears the datatable object that stores the sorted defaultview
sortedData.Clear();
//grabs column names the user has selected to sort by and copies them to a string[]
string[] lbItems = new string[lbColumnsToSortBy.Items.Count];
lbColumnsToSortBy.Items.CopyTo(lbItems, 0);
//adds temp columns to data to sort numerical strings properly
string[] itemsToSort = AddSortColumns(lbItems);
//creates parameters for defaultview sort
string columnsToSortBy = String.Join(",", itemsToSort);
string sortDirection = cbAscDesc.SelectedItem.ToString();
DataView userSelectedSort = data.AsDataView();
userSelectedSort.Sort = columnsToSortBy + " " + sortDirection;
//copies the defaultview to the sorted table object
sortedData = userSelectedSort.ToTable();
RemoveSortColumns(itemsToSort);//removes temp sorting columns
}
}
Instead of sorting on data.DefaultView I create a new DataView object and pass data.AsDataView() as it's value then sort on that. Completely gets rid of the issue in my original code. For anyone wondering I still believe it is bug with .DefaultView in the .NET framework that Microsoft will probably never fix. I hope this will help someone with a similar issue in the future.
Here is the link again to where I figured out a solution to my problem.
https://stackoverflow.com/a/5665600
Having some issues with some of my code that has brought me to a brick wall. Even after searching a few hours I cant seem to get it to work. I am quite new to c# so any advice at all even if it points me in the right direction would be greatly appreciated!
What's odd is that it is correctly looping through the dataset, however when it finds the correct row.ItemArray[0].Equals(changeUserName) it does not continue the code and instead skips past it to the "Change failed" error, wont even show the messagebox!
Basically what I am trying to accomplish is this:
There is a table with 3 Columns. Username Password and Email. Password Column contains Hashed Passwords.
I have a form with 4 labels and 4 text fields:
Username - changeusername Textbox
Current Password - currentpassword Textbox
New Password - newpassword Text box
Confirm New Password - confirmnewpassword Text Box
The user fills these forms in, then hits the Change Password Button, which starts the below function:
(This function should check the value of username text box (and current password once ran through my HashPass Function) and compare them to the values in the dataset. If it finds matching values, It then should change the Password value to the new password value in newpassword Text Box. However it does not do this and I cannot figure out why!)
Note: Some of the code in //And look for matching usernames is commented out as I was just trying to get it to even show a MessageBox once it finds a matching Username. But it would not even do this!
public void changePass(string changeusername, string old, string new1, string confirmnew)
{
string EncryptedPass = HashPass(new1);
//If there is no username
if (changeusername == null)
{
MessageBox.Show("Please Enter Username!");
return;
}
//Confirm new pass must equal confirmnewpassword.
else if (new1 != confirmnew)
{
MessageBox.Show("New Passwords do not match");
}
bool loop = false;
//loop database and update new password
foreach (DataRow row in <nameremoved>stockDataSet.login)
{
//And look for matching usernames
if (row.ItemArray[0].Equals(changeUserName))
{
//row.ItemArray[1] = EncryptedPass;
MessageBox.Show("Change Success");
loop = true;
return;
}
}
//Catch Error if Failure
if (loop == false)
{
MessageBox.Show("Change Failed");
}
}
You can't change a single item inside the item array using the indexer.
This happens because when you try to access the ItemArray property, a copy of the original array is created and returned to your code.
And you change items in this copy not on the original one.
You need to get the item array returned, change it and assign it back to the ItemArray property
foreach (DataRow row in <nameremoved>stockDataSet.login)
{
//And look for matching usernames
if (row.ItemArray[0].Equals(changeUserName))
{
object[] returnedArray = row.ItemArray;
returnedArray[1] = EncryptedPass;
row.ItemArray = returnedArray;
MessageBox.Show("Change Success");
loop = true;
return;
}
}
You can check this behavior looking at the Reference Source of ItemArray
However, I can't understand why you want to use the ItemArray property in this context. You can simply refer to your row/column using the standard syntax or better use the Select method on the DataTable to find your row
DataRow[] found = login.Select($"userName = '{changeUserName}'");
if(found != null && found.Length > 0)
{
found[0]["Password"] = EncryptedPass;
}
Here I assume that your first column is named userName and you second column is named Password (change them to fit your names)
Using Windows forms, My 'listview' have multiple columns as shown in the picture.
I have been trying to make this txtbox_search to be advanced. When any character, word or number is inserted, i want Some columns of my listview to be traversed to look for the character, word, number and bring up data related to the input.
Like when i enter: txtbox_search.Text = "a"
It should travers column "Name" and fill Listview with data:
entire row that has a name which starts with "a" such as "Anwar"
entire row that has a name which starts with "a" such as "Anas"
so on with entire rows that has a name which starts with "A..."
when i enter: txtbox_search.Text = "1"
It should travers column "ID" and fill Listview with data:
entire row that has a ID which starts with "1" such as "1002"
entire row that has a ID which starts with "1" such as "1112"
so on with entire rows that has a ID which starts with "1..."
so far i have been trying this for 2 days and end up with this much:
private void textBox_DEC_Search_TextChanged(object sender, EventArgs e)
{
foreach(ListViewItem Items in listView_DEC_CustomerList.Items)
{
if(Items.Text == textBox_DEC_Search.Text)
{
listView_DEC_CustomerList.Items.Clear();
listView_DEC_CustomerList.Items.Add(Items);
}
}
if(textBox_DEC_Search.Text == "" || textBox_DEC_Search.Text == string.Empty)
{
CusList Cus = new CusList();
Cus.CustomersList(listView_DEC_CustomerList);
}
}
This code only travers first column and bring up data that matches the inserted ID, only if the Complete ID matches with txtbox_search.Text how can i make this possible? (i want it to be on client side, not from sql/database). Guides and sample code helps will be really appreciated. Thanks.
To distinguish between your 2 criteria you could use the following:
if (textBox_DEC_Search.Text.All(x => Char.IsNumber(x)))
{
Debug.WriteLine("Number");
// search through ID
}
else
{
Debug.WriteLine("Name");
// search through Name
}
It basically checks whether your input is solely numeric.
EDIT:
To check for similarity you cold use String.StartsWith of String.Contains to make the search a little more flexible
to look for the ID or NAME you need to access the subitems!
since ID is your first column check SubItems[0]
if(Items.SubItems[0].Text.StartsWith(textBox_DEC_Search.Text) ||
Items.SubItems[0]Text.Contains(textBox_DEC_Search.Text))
since NAME is your second column check SubItems[1]
if(Items.SubItems[1].Text.StartsWith(textBox_DEC_Search.Text) ||
Items.SubItems[1]Text.Contains(textBox_DEC_Search.Text))
One Problem is this line:
listView_DEC_CustomerList.Items.Clear();
because it will erase the first found result when the second is found.
So if you find 10 matches the previous 9 will be deleted!
I suggest to make first the entire search and then add the results if there are any:
private void textBox_DEC_Search_TextChanged(object sender, EventArgs e)
{
// index is 0 if numeric for ID or 1 if not for NAME
int ind = textBox_DEC_Search.Text.All(x => Char.IsNumber(x)) ? 0 : 1;
List<ListViewItem> matchlist = new List<ListViewItem>();
foreach(ListViewItem Items in listView_DEC_CustomerList.Items)
{
if(Items.SubItems[ind].Text.StartsWith(textBox_DEC_Search.Text) ||
Items.SubItems[ind]Text.Contains(textBox_DEC_Search.Text))
{
matchlist.Add(Items);
}
}
// if you have found something add the all results
if(matchlist.Any())
{
listView_DEC_CustomerList.Items.Clear();
listView_DEC_CustomerList.Items.AddRange(matchlist.ToArray());
}
}
Disclaimer: Although this solution should work I would vote to follow the advice of #RezaAghaei. It is less messy and confusing than directly manipulating the ListView
Instead of using == which looks for an exact match try one of the following (I am assuming 'Text' is the column name in the list containing the name - if not change it to Items.Name (for example)
if you want to search on 'starting with' then try
if (Items.Text.StartsWith(textBox_DEC_Search.Text.Trim())
if you want to search based on the fact that a part of the string should be looked up then try
if (Items.Text.Contains(textBox_DEC_Search.Text.Trim())
You can similarly do for any other column you would like to search on. if you want to make the search case insensitive then use .ToLower() on the string and the column name.
I am trying to retrieve data from an Excel spreadsheet using C#. The data in the spreadsheet has the following characteristics:
no column names are assigned
the rows can have varying column lengths
some rows are metadata, and these rows label the content of the columns in the next row
Therefore, the objects I need to construct will always have their name in the very first column, and its parameters are contained in the next columns. It is important that the parameter names are retrieved from the row above. An example:
row1|---------|FirstName|Surname|
row2|---Person|Bob------|Bloggs-|
row3|---------|---------|-------|
row4|---------|Make-----|Model--|
row5|------Car|Toyota---|Prius--|
So unfortunately the data is heterogeneous, and the only way to determine what rows "belong together" is to check whether the first column in the row is empty. If it is, then read all data in the row, and check which parameter names apply by checking the row above.
At first I thought the straightforward approach would be to simply loop through
1) the dataset containing all sheets, then
2) the datatables (i.e. sheets) and
3) the row.
However, I found that trying to extract this data with nested loops and if statements results in horrible, unreadable and inflexible code.
Is there a way to do this in LINQ ? I had a look at this article to start by filtering the empty rows between data but didn't really get anywhere. Could someone point me in the right direction with a few code snippets please ?
Thanks in advance !
hiro
I see that you've already accepted the answer, but I think that more generic solution is possible - using reflection.
Let say you got your data as a List<string[]> where each element in the list is an array of string with all cells from corresponding row.
List<string[]> data;
data = LoadData();
var results = new List<object>();
string[] headerRow;
var en = data.GetEnumerator();
while(en.MoveNext())
{
var row = en.Current;
if(string.IsNullOrEmpty(row[0]))
{
headerRow = row.Skip(1).ToArray();
}
else
{
Type objType = Type.GetType(row[0]);
object newItem = Activator.CreateInstance(objType);
for(int i = 0; i < headerRow.Length; i++)
{
objType.GetProperty(headerRow[i]).SetValue(newItem, row[i+1]);
}
results.Add(newItem);
}
}
I have a parent WinForm that has a MyDataTable _dt as a member. The MyDataTable type was created in the "typed dataset" designer tool in Visual Studio 2005 (MyDataTable inherits from DataTable) _dt gets populated from a db via ADO.NET. Based on changes from user interaction in the form, I delete a row from the table like so:
_dt.FindBySomeKey(_someKey).Delete();
Later on, _dt is passed by value to a dialog form. From there, I need to scan through all the rows to build a string:
foreach (myDataTableRow row in _dt)
{
sbFilter.Append("'" + row.info + "',");
}
The problem is that upon doing this after a delete, the following exception is thrown:
DeletedRowInaccessibleException: Deleted row information cannot be accessed through the row.
The work around that I am currently using (which feels like a hack) is the following:
foreach (myDataTableRow row in _dt)
{
if (row.RowState != DataRowState.Deleted &&
row.RowState != DataRowState.Detached)
{
sbFilter.Append("'" + row.info + "',");
}
}
My question: Is this the proper way to do this? Why would the foreach loop access rows that have been tagged via the Delete() method??
The Delete method marks a row for deletion; the row is not actually removed until you call AcceptChanges.
Instead, call _dt.Rows.Remove(_dt.FindBySomeKey(_someKey)), which will also accept the change.
Believe it or not, Rows.Remove will completely remove the row, whereas row.Delete() won't.
Note that if you call Rows.Remove, the row will be permanently gone, and will not be deleted from the database by a DataAdapter.
In C# 3, you can replace your if statement with the following extension method:
///<summary>Gets the rows in a typed DataTable that have not been deleted.</summary>
public static EnumerableRowCollection<TRow> CurrentRows<TRow>(this TypedTableBase<TRow> table) where TRow : DataRow {
return table.Where(r => r.RowState != DataRowState.Deleted);
}
foreach (myDataTableRow row in _dt.CurrentRows())
{
...
EDIT
Here's a C# 2 version:
static class Utils {
public static IEnumerable<TRow> CurrentRows(IEnumerable<TRow> table) where TRow : DataRow {
foreach(TRow row in table) {
if (row.RowState != DataRowState.Deleted)
yield return row;
}
}
}
foreach (myDataTableRow row in Utils.CurrentRows(_dt))
{
You could also put this function in the partial class for the typed table:
partial class MyTable {
public IEnumerable<MyRow> CurrentRows() { return Utils.CurrentRows(this); }
}
What you're doing to skip the rows in your iteration is correct, I routinely check the RowState when I loop over a DataTable that may be modified.
In some cases I believe you want the original row value before the row was marked as deleted. There is a secondary index option when retrieving a particular datarow value.
string st = dr["columnname", DataRowVersion.Original].ToString(); // c#
dim st As String = dr("columnname", DataRowVersion.Original).ToString() ' vb.net
I've gotten stuck on this one several times in the past.
As far as the GetChanges(RowState) method is concerned, don't forget to to check for a null return, if there are no rows of that RowState, the DataTable returned is null (I think it should return a table with zero rows)
You will have to check:
_dt.DefaultView.RowStateFilter
I think that the default setting won't show deleted rows, maybe you change it somewhere.
You can always create an extra RowView and control its filter and do your loop over the View.
Use GetChanges():
foreach (myDataTableRow row in _dt.GetChanges(DataRowState.Added))
{
sbFilter.Append("'" + row.info + "',");
}
If you want to get all added and modified, use bitwise OR on Added and Modified:
foreach (myDataTableRow row in
_dt.GetChanges(DataRowState.Added | DataRowState.Modified))
{
sbFilter.Append("'" + row.info + "',");
}
I'm not sure of this, but I think if you call _dt.AcceptChanges() after deleting one or more rows, you will not "see" the deleted rows when you iterate through the data tables's Rows collection.