10 Replies - 2760 Views - Last Post: 20 December 2011 - 02:17 PM Rate Topic: -----

#1 nighttrain   User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 485
  • Joined: 22-September 10

Populate/refresh datagridview control

Posted 18 December 2011 - 11:23 AM

On first Form I got datagridview which I populate some data, I have here a button to Delete some record from datagridview, when I click on it I open a new form, when I got some controls where I can change something in my database. What I want is when I update data and click button on my second form. I want to Close that form, and make my datagridview on Form1 to populate a records again. The populate function on Form1_Load is:
 public void PopulateGridClients()
        {
            grdClients.DataSource = DAL.DataAccessLayer.ExecuteDataTable("SELECT * FROM tb_klient");
        }



On second Form I try to do like this but it's not work (look at finally statment)
private void btnUpdate_Click(object sender, EventArgs e)
        {
            .....

            try
            {
                connection.Open();
                ....
                cmd.ExecuteNonQuery();
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                ....
            }
            catch (System.Exception ex)
            {
                .....
            }
            finally
            {
                connection.Close();
                [b]
                frmClientView r = new frmClientView();
                r.PopulateGridClients();
                r.Refresh();
                this.Close();[/b]
            }
        }



Is This A Good Question/Topic? 0
  • +

Replies To: Populate/refresh datagridview control

#2 RexGrammer   User is offline

  • Coding Dynamo
  • member icon

Reputation: 183
  • View blog
  • Posts: 785
  • Joined: 27-October 11

Re: Populate/refresh datagridview control

Posted 18 December 2011 - 11:50 AM

So your question is really about the interaction between two forms?

If so then take a look at this:
How to Get Values From Form1 to Form2 (or any other forms) (DIC Tutorial)

This could also help you:
Quick and easy custom events (DIC Tutorial)

Basically how I would do this is:
1. Create an event that will be triggered by some action (in your case when you do your logic in the second form)
2. Make a event handler on the first form for that event
3. Do my logic there

(Look at the tutorial at the link I gave you for more info)

This post has been edited by RexGrammer: 18 December 2011 - 11:53 AM

Was This Post Helpful? 0
  • +
  • -

#3 nighttrain   User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 485
  • Joined: 22-September 10

Re: Populate/refresh datagridview control

Posted 18 December 2011 - 12:09 PM

I tried to do it like this but it's now work, and wrong syntax what i do wrong?:
public frmClientEdit()
        {
            InitializeComponent();
            frmClientView cv = new frmClientView();
            cv.PopulateGridClients += new EventHandler<NameUpdatedEventArgs>(cv.PopulateGridClients);
        }

        void cv.PopulateGridClients(object sender, NameUpdatedEventArgs e)
        {
            throw new NotImplementedException();
        }


Was This Post Helpful? 0
  • +
  • -

#4 RexGrammer   User is offline

  • Coding Dynamo
  • member icon

Reputation: 183
  • View blog
  • Posts: 785
  • Joined: 27-October 11

Re: Populate/refresh datagridview control

Posted 18 December 2011 - 01:20 PM

Is it working now or not?

Also just a note:

In this code:

catch (System.Exception ex)
	            {
	                .....
	            }



You could only write:
catch (Exception ex)
	            {
	                .....
	            }



Since you probably have the using System; directive.

NOTE: You don't have to use fully qualified names when you have using directives.
Was This Post Helpful? 0
  • +
  • -

#5 nighttrain   User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 485
  • Joined: 22-September 10

Re: Populate/refresh datagridview control

Posted 19 December 2011 - 01:36 AM

I know it but I do it in my BAL layer. No i still got problem with it please of help
Was This Post Helpful? 0
  • +
  • -

#6 RexGrammer   User is offline

  • Coding Dynamo
  • member icon

Reputation: 183
  • View blog
  • Posts: 785
  • Joined: 27-October 11

Re: Populate/refresh datagridview control

Posted 19 December 2011 - 03:45 AM

Quote

I tried to do it like this but it's now work, and wrong syntax what i do wrong?


I can't understand this. You say 'it's now work, ...', from this I think it works, then '..., and wrong syntax what I do wrong?', from this I think it doesn't work.

Also is there a run-time error or a compile error?

We're having serious language issues, maybe try using Google Translate to translate what you want to say from your native language to English.
Was This Post Helpful? 0
  • +
  • -

#7 nighttrain   User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 485
  • Joined: 22-September 10

Re: Populate/refresh datagridview control

Posted 19 December 2011 - 02:29 PM

I try to explain a bit more clear.

On first Form (frmClientView) I have got function which I populate datagridview:

public void PopulateGridClients()
        {
            grdClients.DataSource = DAL.DataAccessLayer.ExecuteDataTable("SELECT * FROM tb_klient");
        }



On second Form (Form2) I Edit some data, and If i click button, I want to refresh datagridview from Form1, so I try to do it like this:

button click:
......
finally
            {
                connection.Close();
                
                [b]frmClientView r = new frmClientView();
                r.PopulateGridClients += new EventHandler<NameUpdatedEventArgs>(r.PopulateGridClients);
                this.Close();[/b]
            }



on Form2 I got this too:
void r.PopulateGridClients(object sender, NameUpdatedEventArgs e)
        {
            throw new NotImplementedException();
        }



But I retrive those errors:
Error	4	The type or namespace name 'r' could not be found (are you missing a using directive or an assembly reference?)

Error	1	The type or namespace name 'NameUpdatedEventArgs' could not be found (are you missing a using directive or an assembly reference?)

Error	3	The type or namespace name 'NameUpdatedEventArgs' could not be found (are you missing a using directive or an assembly reference?)

Error	2	No overload for 'PopulateGridClients' matches delegate 'System.EventHandler<NameUpdatedEventArgs>'


This post has been edited by nighttrain: 19 December 2011 - 02:30 PM

Was This Post Helpful? 0
  • +
  • -

#8 RexGrammer   User is offline

  • Coding Dynamo
  • member icon

Reputation: 183
  • View blog
  • Posts: 785
  • Joined: 27-October 11

Re: Populate/refresh datagridview control

Posted 19 December 2011 - 02:37 PM

Ohhh, really sorry for that. I must've been drunk while reading your posts, since I couldn't understand them.

The solutions:

1. Use fully qualified names for NameUpdatedEventArgs (or add a using directive).
2. I think your methods can't have dots in their names. So instead of naming your method like: r.PopulateGridClients, name it: rPopulateGridClients (or anything else without a dot(.))
Was This Post Helpful? 0
  • +
  • -

#9 nighttrain   User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 485
  • Joined: 22-September 10

Re: Populate/refresh datagridview control

Posted 19 December 2011 - 04:14 PM

Could U show me how it will look like on my case, I am really roll up with this ;/
Was This Post Helpful? 0
  • +
  • -

#10 RexGrammer   User is offline

  • Coding Dynamo
  • member icon

Reputation: 183
  • View blog
  • Posts: 785
  • Joined: 27-October 11

Re: Populate/refresh datagridview control

Posted 20 December 2011 - 06:17 AM

Change this:

void r.PopulateGridClients(object sender, NameUpdatedEventArgs e)
	        {
                       //...
                }



into this:

void rPopulateGridClients(object sender, NameUpdatedEventArgs e)
	        {
                       //...
                }


It's as simple as that!

And I'm not sure for 'NameUpdatedEventArgs', since it is a child of EventArgs which is in the System namespace, I suggest adding the using System; directive or statement if you want.

using System;


Was This Post Helpful? 0
  • +
  • -

#11 nighttrain   User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 485
  • Joined: 22-September 10

Re: Populate/refresh datagridview control

Posted 20 December 2011 - 02:17 PM

I did the way:
finally
            {
                connection.Close();
                
                frmClientView r = new frmClientView();
                PopulateGridClients += new EventHandler<NameUpdatedEventArgs>(r.PopulateGridClients);
                this.Close();
            }



void PopulateGridClients(object sender, NameUpdatedEventArgs e)
        {
            throw new NotImplementedException();
        }



And I already have an using System reference. And retrive thise error in here:
void PopulateGridClients
The type or namespace name 'NameUpdatedEventArgs' could not be found (are you missing a using directive or an assembly reference?)



regards and thanks for helping
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1