Creating a table and bind it to an array - c#

I'm pretty new at this programming thing...and I need to make a program which lists some customers and they need to be clickable so I can press a button to delete the selected item.
How do I create a table which will show all my objects from an array, and would make it easy to mark one item and click on a delete button?

I'd suggest using a DataGridView, which you can bind to your data source (you might have to store the information in an ObservableCollection if it needs to be dynamic). Then just have your button delete the selected row (make sure to disable the button if no rows are selected).

Like an alternative to #Nick answer I can add ListView control in Details view mode. It's much lightweight then DataGrid, but also provides less functoonality. It's a metter of choice. If you don't need complicated stuff, but just: select row, delete row, may be search row, ListView could be pretty suitable option. But, I repeat, keep atention on your app requierements.
Regards.

Related

WPF - what is the best way to bind data from a database to a checkbox control

I am struggling some time with checkbox control in WPF. What I am trying is to to make some kind of filter.
1. So first I need to fill the checkbox control with the database items
2. Second I need to check if anything is checked
3. Third if anything is checked I want to take those values and use them to search through the database
4. Should I use only checkbox control for this or should I put it in some container or a listBox?
I have find many topics here that are mentioning this stuff, but I couldn't manage to find the complete answer, only lot of parts that are not compatible with each other. I would really appreciate if someone explain to me how checkbox in WPF works.
If you have multiple values and you want to display multiple CheckBoxes, one for each of them, then you will need to use a ListBox and have the TemplateItem a CheckBox. This way your collection of items is bound to the ListBox, and for each item in the ListBox a CheckBox is shown.
So to get each of the values after there is an additional step if your doing MVVM and don't want to touch the UI. What you do is create a wrapper class that sits around your class and has a extra property for the IsChecked data. This way you can get the checked state without touching the UI.
I have an example of this on my blog:
Checked ListBox in WPF
To check which items are checked you just need a simple LINQ query. So if you use the example there from my blog then you should be able to do something like this:
var checkedCustomers = Customers.Where(w=>w.IsChecked).ToList();

WPF dataset/database row edit in new window

I know that there is a lot of questions about databinding in WPF C# but I'm already out of my mind.
There is a simple way to do a databinding to a dataGrid in wpf, it is a built-in feature.
But I want to click on the particular row, then click edit button and edit this row and some additional info in new popup window, not into datagrid itself.
Selecting particular row is a other story but now the main problem for me is to load data from this particular row in database to textboxes in new window.
I can do this by
DataTable obj = contactsTableAdapter.GetOne(id);
DataRow r = obj.Rows[0];
String sur = r[5].ToString();
but this is wrong and one way solution.
I want to use a proper databinding between this selected row and my textboxes, where the user can modify data and click button to call Update method to propagate changes to database.
I'm not sure how to load that particular selected row, to what type of object, and how to bind it to textbox, and how to do this in a clean way, without doing some hacks.
I'm pretty sure that c# provides some obvious solution but I have lost my way somewhere.
I'm waiting for some help impatiently, and thanks for any solution!
If you don't want to use MVVM here is a simple solution.
Your button click method should have access to datagrid
MyItemType item = (MyItemType)myDataGrid.SelectedItem;
Pass the item to your new form\window\whaterver.
NOTE: You might want to null check the selecteditem. You can put the edit buttons in your DataGrid Row.

How to make popup window auto populate with contents in cell

I have a data grid displaying results from a sql database. What I want to do is make it so that when I double click on a cell in the datagrid, a new window appears where that cell can be edited. I also would like to have all the info for that cell to be autogenerated into editable fields when the cell is double clicked. Any help or links to tutorials is appreciated. Thanks.
Try this DataGridView.CellDoubleClick Event
Did you consider using data binding? Simply put, you can bind your items list to DataGrid, and current item (eg. selected/double clicked) to your edit form's controls (TextBox, Label and whatever else you might have there).
Here are good starting points for windows forms: #1, #2. They cover problem you're facing. You can find data binding tutorials for ASP/WPF easily too, but especially first link covers entire concept pretty well.

How can I have my listbox have columns?

I want my ListBox to have columns, and one of those columns have to be a clickable URL.
How can I achieve this?
You can't do it in a ListBox. You can create your own control, or settle for another existing one. Based on the question, I'd guess you're not yet at the stage where you're creating your own controls. That takes a pretty good understanding of existing controls and the way they work under the covers (but a google search for creating Winforms Controls should yield plenty of instructions.) Edit added It looks like te 4th and 5th links in combination on that google search should get you what you need. You can create your own user control and then do an array of them)
As far as for other possible alternatives, have you considered a DataGridView? A DataGridView can have a hyperlink and it can have checkbox columns, so this would be one possible alternative.
Here's a link for having a Hyperlink column in a DataGridView.
Well, it is possible by using the CustomTabOffsets property (unreliable) or the DrawItem event. And implementing the MouseDown event to find out if that particular 'column' was clicked.
But there's little point, a ListView control with View = Details gives you the same functionality.

C# dropdownmenuitem clicked and listview editing

I have 2 questions concerning C#.
1) I have a dropdown menu with several items in them. They are clickable, but when I click one, the older clicked one stays selected. Click another and the 2 original ones stay selected, and so on. I don't want this. What I want is that when I click one of the dropdownitems, that one is that selected one and the others are not.
2) I have a listview items on a winform. I loaded some string elements into it from a file. Now what I want to do is to be able to edit those strings and even add strings, just by clicking on the rows in which the data goes.
I've checked google and MSDN for these problems, but nothing helps, so I turn here.
2) The ListView does not support that type of action. You can roll your own (pain in the #$$), or perhaps a DataGrid would be better suited to your purpose.
EDIT:
This link may help
This one too
For #1 I'm a little confused. If the DropDownStyle isn't set to simple something strange is occuring. It's not much but maybe you could try recreating the control.
For #2 the easiest solution I can think of is to set a TextBox to be equal to the selected text value from your listview. After that write a little function to update the selected index of the listview with the edited text from the listview.
Please comment if you have any more information about #1.

Categories

Resources