Form reloading in C#.net - c#

I have a form i am fetching many records from database that are rendering dynamical on from. When i am clicking on Delete button this is deleting as well but I want to re-render my form so user will feel that records is deleted completely. when i am writing code this.refresh(); this is not fetching values from database and i am not seeing record is completely gone. It is windows C# form.

The methods Form.Refresh and Form.Invalidate have nothing to do with causing the application to retrieve new data from the database.
Form.Refresh - redraws the form synchronously (i.e. Form.OnPaintBackground and Form.OnPaint are called directly on the current thread).
Form.Invalidate - redraws the form asynchronously (i.e. WM_PAINT message is sent to the window, so Form.OnPaintBackground and Form.OnPaint will be called by the UI thread when it handles messages).
In order to update records, you need to make your control retrieve updated data.
If you specify more details on how the Form pulls that data, someone can help you figure out how to update it. For example, are you using data binding? Did you write your own code to retrieve the records?
Basically, without seeing your code, I would say you can take whatever it is you are doing in the constructor and put it in a separate method (let's call it RefreshData). Then, call RefreshData after you delete records from the database.
So, you're creating new controls that represent the data. In that case, there's no mechanism to automatically do the updating work for you, you'll have to code it. You generally have two options:
Keep a reference to the controls you created, and remove them from the Form before updating it.
Implementing something more efficient, like maintain a Dictionary that maps from the Data Rows to the controls that represent it, so that you can go over the updated data, and for each row in your dictionary that disappeared from the data, remove the controls.
You may also want to consider using some control that has a built-in Data Binding mechanism that would save you lots of work. For example, a GridView.

Could you be looking for Invalidate() ?

If you are filling the form in constructor you need to extract loading code to a method.
Then call it on DeleteButton.OnClick event after performing delete.

Related

Too much data in C# windows form combobox, taking 30 sec to show the windows form

I am creating a windows form, that contains a simple combo box, which has around 100000 items. After loading all those items when I try to show the form useing showDialog() method it is taking 30 sec to show the form.
Is there any way that I can show it immediately ?
As pointed out by #jmcilhinney, it is not practical to load all this data to a combobox. Your system could (and it is) getting unresponsive on other situations.
There might be a lot of reasons for you have to do this, and since you are not disclosing everything, I can give a suggestion to answer your question - remember, not the good way, but would make things better for a while.
You might be binding your information on your load process. Separate it, make it async.
There are several ways of doing this, like making an async call to a internal method and set your ComboBox.DataSource or ComboBox.DataBind.
Also, on the end of your load/initialization of your window, set the cursor do hourglass/wait. Then set it back to the original one (might be the normal pointer). This would give the effect that the dialog is shown but it is yet loading. It is a lot better for the user showing that you are loading info.
But after that, try filtering data as you type, as suggested by #jmcilhinney. Even if it a fixed list, make it editable and filter data. Don't let the user see more than +1000. You should find a way to show the user that has too much thing and provide means to make your data filterable.
If you have data loading code for combobox in the load event, which I think you do. Move that code to a separate function and call that from a timer or thread. Or you can make the function async and then call that function from load event. This will not keep your form waiting till all the data is loaded in the combobox.

c# ammend SQL data

I am making a basic ticketing system in C# with basic coding experience. Most of my experience is in SQL.
I have the database and tables. I have stored procedures to create and amend tickets.
I am stuck (this is probably very basic) because:
On my EDIT ticket page, I populate various text boxes and drop downs from my existing data via inline SQL.
On my page, I can edit all the fields and dropdowns with new values. (i.e. change a ticket priority from when it was first logged)
I have a button, that calls my "update" stored procedure, however it updates only the NEW fields I have, any amendments to the existing fields are overwritten by the original values before submitting.
The original values are called on pageload, so I think the button reloads the page before submitting. I want it to submit all the values that are on the screen.
I think this must be something obvious, remember I am a novice so I may have missed something simple.
If what you're saying is you load values from the DB into the form controls in the PageLoad event handler, then yes, you're probably overwriting the changed values. To keep things as simple as possible for you, wrap the original values loading code in the following:
if( !IsPostBack )
{
// load initial form values here from DB
}
I'd suggest you read about the ASP.NET page lifecycle: http://msdn.microsoft.com/en-us/library/ms178472(v=vs.90).aspx

Does DataGridView crash when enough items is added that it expands beyond the window?

I have a DataGridView which I populate with values as soon as they are returned from an Udp-server. When there are only a few values my program works, but if enough values are retrieved that the dialogbox needs to expand outside the dialogbox, the DataGridView freezes.
I've created this simple method for populating the DataGrid.
public void populateGrid(string[] data)
{
dataGrid.Rows.Add(data);
}
The Winforms DataGridView should indeed be able to expand easily beyond what's visible. One of the programs I wrote routinely has thousands of items in some of its DGVs, and DGVs are used everywhere in that software. I populate the lists using the Rows.Add() method, not unlike what you're doing.
The question becomes, how are you using this populateGrid method? Are you looping through calls, or are you using controls with user-entered data to form the array for each row? How is the GridView set up (readonly? Can the user add/delete rows?) Are there events to respond to resizing of DGV elements? Are you "lazy-loading" the records from some data source to populate this DGV?
Try attaching the debugger and hitting the "pause" button to see where the system is hanging. You may see repetitions in the call stack indicating an infinite loop of event handlers, or it may be hanging on a method call you expected to be fast but is waiting for a troublesome data source or pulling MUCH more info than you need.
I'm not exactly sure how or why, but by creating a backgroundworker and invoking a delegate on the DGV I accomplished adding items without the system freezing, and the backgroundworker reaches it's Completed event.

Persist c# objects across postbacks

I've got an asp.net page that has c# code-behind that does some stuff in the Page_Load() method (like query a database and make a few other calls to populate objects with data). I then display this data on the page. This all works fine. I set up a couple of postbacks so that when a value in a listbox is clicked, a panel control is filled with the rest of the corresponding object's data. I thought postbacks were the right way to do this, but this causes the (entire class?) to be re-called, which re-initializes my objects and destroys the data I want to keep.
Will some form of partial-postback solve this problem, or is there a better way to implement what I'm trying to do?
I don't want to re-populate the objects every time a postback is called, as that takes a database query, and I want to avoid re-querying every time something is clicked...
I've found many questions regarding persisting Javascript objects, but nothing that really seems to address this. I'm using .Net 4.0
Put the objects into the Session for the current user.
Put all your initialization stuff in an (!IsPostback) { } and use partial postbacks. That way the initialization code doesn't get called again during the postbacks.
Why don't you cache the object?
Caching API, Using the Cache Object:
http://msdn.microsoft.com/en-us/library/aa478965.aspx#aspnet-cachingtechniquesbestpract_topic4

Return updated POCO object attached to GridView control using Reflection

I have a Custom GridView Control derived from ASP GridView Control. Now on all the Forms i have been using my CustomGridView Control.
I attach a List object to DataSource of this CustomGrid, and also store this List into ViewState so that i can manipulate this object when ever grid performs any postback.
Now every time i do insertion, updation and deletion i have to manually write code for Data Manipulation in my all Form. i.e. retrive data from ViewState and then update data or delete data or insert data into that object and store it again into ViewState.
I want a way to automatically write Code in my CustomGridView class object i.e. at one place to take care of this task. My CustomGridView Control should able to update, insert and delete for all my forms without Form writing any code at all.
In the end when its time to save the data into persistent storage i should get the final POCO object from CustomGridView Control.
This can be achieved using reflection but how?
Can you please tell me how can i achieve this task? Or what would be the best approach that i should follow?
Thanks,
Huzefa
I question whether it is best practice to store your underlying datasource within the view state. Have you considered putting this list in the session instead? It would be easier to access and you wouldn't burden your users with inflated page sizes due to overweight viewstate.
If you need to go with your stated approach, could you consider putting the code into a base form instead of trying to wedge it in the gridview? That way you only need to write the code once but it's more reasonable than trying to hack the gridview itself.

Categories

Resources