Edit a bound DataTable in Visual Studio GUI Dsigner? - c#

I am extremely new to c# and WinForms, so I apologize in advance if this is something trivial.
With my mouse only, I created a form in the Visual Studio Designer and added a DataGridView to it. Then via code, I created a DataTable, added a few columns, added a bunch of generated rows, and bound it to the DataGridView with the following command:
dataGridView.DataSource = dt;
The program works just fine. However, in the Designer I see the DataGridView empty, and cant make any column related changes such as changing a column color, changing its size, etc.
Note that I DO know the column names and types in advance. It is only the data itself which is dynamic and generated on the fly.
Please help!

I'm know for sure you can change make your column related change in the backend code.
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview_properties(v=vs.110).aspx
You would need to iterate trough your grid and change every row/columns/cell properties.
`Dim test As New DataGrid
test.Columns(0).ItemStyle.BackColor = Drawing.Color.AliceBlue`
This is vb code, but it would be pretty much the same thing in every language.
Otherwise, to use the GUI you can go in the properties->style
Exemple :
http://imageshack.com/a/img837/4024/hvda.png

Related

DevExpress WPF GridControl with dynamic columns and rows

I'm working with a DevExpress 14.1 GridControl on WPF, which must bind to a dynamic source. We don't know the number of columns or rows on design time, so this must be calculated on the fly.
The source may be changed while executing, adding more rows, or columns, or BOTH (I could have a table with 3 columns and 5 rows, and a 6th row with 4 columns could be inserted, adding a new column to the model with empty data for the previous rows).
I was using a DataTable as ItemsSource for the grid, but it will only load data inserted on design time. If I add columns while running the app, the grid wont update for some reason.
Is there an observable object that can satisfy this needs?
It's using code-behind (not MVVM) and maybe you already tried it, but have you tried explicitly calling myGrid.RefreshData() in your .xaml.cs file? There'd be some hook-ups to get it to call at the right times depending on your data changes, but it might at least help you narrow in on the problem if it helps or not.
DevExpress support answer here may help also.
Changing from a DataTable to an ObservableCollection may also do the trick: see this.
BTW, I've found DevExpress's support ticket system to be very helpful; they seem to respond within ~24 hours for issues and questions. If you're still able to get support for your license & can't solve it still, I'd ask them the same question here.

C# : Resetting DataGridView for new DataTable

Ok so I'm trying to make a simple SQL CE Viewer application just to view my local databases for another application, I have it setup so that I can select what database I want to open and then it automatically populates a combo box with all of the tables in the database. When I select a table it populates a DataGridView with the records in the table.
My problem is switching between tables. I can't seem to get the DataGridView to remove everything from the previous table and re-populate the DataGridView with the new table information. Of course each table has different columns and rows and such.
I've googled and searched on here and every suggestion I find doesn't seem to work. It populates the DataGridView with the first table just fine, but when I select another it basically adds the columns and rows into whatever was there....
How can I get the DataGridView to completely clear for new data?
And please don't tell me to use dataGridView1.DataSource = null; tried that, doesn't work.
Well, I checked it in one of my programs and just changing DataSource property is working fine. I didn't have to use datagridview.Refresh(). Maybe it depends on the kind of DataSource, which you are using to set datagridview data?
Write this line after you have done populating the DataSource with the new data
dataGridView1.Refresh();
You don't need to clear the DataGridView directly. Its always handled by modifying the DataSource of the DataGridView.
If there is nothing else in the form, you can simply call InitializeComponent() again. But I think #Josh is right.

Using report viewer, how do I pull from two seperate Datasets

I have two datasets I need to pull from, A base that both reports use and then a separate one that only one report pulls from. I get the error
Error 12 The Value expression for the text box ‘Textbox9’ refers to
the field ‘Name’. Report item expressions can only refer to fields
within the current dataset scope or, if inside an aggregate, the
specified dataset scope.
My best guess is I have to associate them with the correct dataset but I have not been able to find any documentation on this.
edit: I am trying to access property files that I created for the fields on the report document.
Can someone please tell me where in the rdlc document I need to code something like name.value, "dataset1" or something similar?
When you create a table in a RLDC, in the Tablix properties (selecting a row or a column) you must associate a DataSet.
After doing that, you have to write in each cell the name of the field (in the dataset) that you will use. You can do that by clicking on the "little table" in the cell, when you put the mouse over it
In images (with Visual Studio 2010)
If you don't see the dataset in the list, you must add it.
For that, click on view menu -> report data.
Then, in the DummyDataSource, click Add Dataset..
And select it from the list, or create a new one instead, in the same form.
If this doesn't work, well I don't know haha
If the two datasets has the same structure, then you could use one just, and in the code assign it to the datasource
I ran into this same error and the only way I could resolve it was by closing and reopening BIDS and then refreshing the fields from the stored procedure (Right click the data set -> Dataset Propoerties -> Query -> Refresh Fields).
Thanks for the troubleshooting tips!
If you are trying to embed the results of one set into a table that is using another data set, have you considered a subreport?
After some careful research and error checking I found that I had to create my base model property file and when I drag the actual data points on to my form I had to associate them with their correct set:
I clicked the value and associated the correct dataset.

DataGridView binding after replacing column with comboboxcolumn

I'm in C# Express 2010 and SQL Server Express 2008, making a winforms front end for simple table editing. User picks a table from a combobox, and a datagridview is populated with that table. There's a Submit and Reload button at the bottom of the form. It's loosely based on Microsoft's example for binding a DGV to a database.
So, I have this datagridview that is populated by a data adaptor. After it's been populated, I go through the resulting table and replace any columns with foreign keys with comboboxcolumns so it's easier for the user.
This bit all works fine. The comboboxcolumn shows up, with the correct data in it etc etc. I've set the Headertext, DataProprtyName, and Name properties to match the column it has replaced.
It DOES seem to have broken my 'submit' button that does the update command. Basically, if I change the value in one of the comboboxcolumns, I get a concurrency violation. If I change a value in any other column, it silently fails (when the data is reloaded into the DGV, the updated value vanishes).
Any ideas what the problem could be here?
If you need to see code just let me know. There's rather a lot of it as I'm a newbie and have probably done things in a horrendously messy way!
Thanks in advance.
Never mind - I figured it out - I had copy/pasted a procedure and forgotten to create a new DataAdaptor, so it was using the same one as the one used to populate the DGV, hence things went A over T...

C# Winforms DataGridView with sorting/filtering like Ms Excel

Hi I need a quick solution to do filtering/sorting using the Winforms DataGridView control just as in Excel.
I have reviewed the existing posts on this area but none seems to meet my needs.
I am populating my DataGridView manually - no data binding
The DataGridView columns already support sorting.
I would populate a DataTable with your data and then bind the DataGridView to myDataTable.DefaultView.
You can filter the rows displayed by setting myDataTable.DefaultView.RowFilter.
You could place Textboxes and/or Comboboxes above the DataGridView and update myDataTable.DefaultView.RowFilter as the input/selections change.
If you're looking for a Excel like filtering funcionality, check out this article:
http://msdn.microsoft.com/en-us/library/aa480727.aspx
Why don't use a cheap 3rd-party component? Even if you buy it, eventually it could really save your money. This DataGridView alternative with autofilter works very fast, and unbound mode is its main work mode. Plus it supports Excel-style AutoFilter.
Do you want something like this?
DataGridView-AutoFilter is a ready-made Nuget package, you just need to download it and follow this article, it is an easy and enhanced approach.
Microsoft has created a sample project for VB and C# where they show how to create what they say is an "auto-filter" plugin. I personally do not like it as it allows filter exact only so product and customer will work, open invoice where price > some value is not implemented
Sorting is implemented by using:
foreach (DataGridViewColumn column in MyDataView.Columns)
{
column.SortMode = DataGridViewColumnSortMode.Automatic;
}
You do this when the grid has it's columns, if auto creating the columns… after binding.

Categories