Click here to Skip to main content
15,922,894 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all

I wanna bypass datagridview to function . but when call function , doesn't return data on datagridview
C#
 public  void Select_all_options_insellGrid(int factor_code,ref DataGridView datagrid)
 {
Factor_sell ff = new Factor_sell();
DataTable dt_check = new DataTable();
DataTable dt_naghd = new DataTable();
DataTable dt_takhfif = new DataTable();
DataTable dt_fish = new DataTable();
dt_check = ff.Select_check_factor(factor_code);
dt_naghd = ff.Select_naghd_factor(factor_code);
dt_naghd.Merge(dt_check,false, MissingSchemaAction.Add);
dt_naghd.AcceptChanges();

 datagrid.DataSource = dt_naghd;
        int p = 0;
            foreach (DataGridViewRow f in dataGridView1.Rows)
            {

         int na = f.Index;
            if (Naghd_radio.Checked == true)
            {
                int j = 1;
                foreach (DataGridViewRow i in datagrid.Rows)
                {
                    int inde = i.Index;
                    datagrid.Rows[inde].Cells[2].Value = Naghd_radio.Text.ToString();
                    datagrid.Rows[inde].Cells[0].ValueType = typeof(string);
                    datagrid.Rows[inde].Cells[2].ValueType = typeof(string);
                    j++;

                } 
            } 
            else if (Check_radio.Checked == true)
            {

                foreach (DataGridViewRow i in dataGridView1.Rows)
                {
                    int inde = i.Index;
                    this.dataGridView1.Rows[inde].Cells[1].Value = Check_radio.Text.ToString();
                    this.dataGridView1.Rows[inde].Cells[0].ValueType = typeof(string);
                    this.dataGridView1.Rows[inde].Cells[1].ValueType = typeof(string);


                }
                    }

                    this.dataGridView1.Rows[na].Cells[0].Value = p.ToString();
                    p++;
                }

          //  datagrid.DataSource = dt_naghd;
            }


and call function in button1_click() event:
C#
 private void button1_Click(object sender, EventArgs e)
 {
  Select_all_options_insellGrid(Convert.ToInt32(factor_num.Text), ref dataGridView1);
}
Posted
Updated 14-Aug-15 2:15am
v3
Comments
Andy Lanng 13-Aug-15 3:55am    
The method probably is firing, but it won't make any changes to the page. Events fire after the page has been redrawn.

A few questions to clarify your issue?
1: Have you debugged the code?
2: Does button1_click fire
3: Does Select_all_options_insellGrid run?
4: What is your end goal?

There are ways to handle events early. An UpdatePanel may be better for you that an early call event.
bernova 13-Aug-15 4:38am    
Hi all
thanks for your response.

my code is run , but doesn't return any data.
Andy Lanng 13-Aug-15 5:14am    
ok - you didn't answer all of my questions, but I can work with that.
Sergey Alexandrovich Kryukov 13-Aug-15 4:04am    
I guess, you mean "pass"... Well, you can pass any parameter you want; how can it be a problem?
—SA
bernova 13-Aug-15 5:21am    
Indeed when change datagridview on function and call function , don't return data on datagridview , but in the normal state , code does run

First of all, remove "ref" from the function declaration; it is totally pointless, even if you want to modify this object. DataGridView is a reference type, so the reference passed by value is quite enough to be passed to a method, even if it modifies the object through the reference.

I'm not sure that "don't fire" makes any sense; you did not mention any events or anything which could "fire", even if you use such jargon. If something else is not working, start with using the debugger.

Now, you really need to learn reference types, references and objects, before doing any programming at all. And of course you have to start to learn OOP, and a lot more. I would not develop any UI or any other advanced topics before you get good understanding of general programming, which is best to exercise on simple console-only applications.

—SA
 
Share this answer
 
v2
Comments
Maciej Los 13-Aug-15 4:17am    
Sure, a 5!
Guess: who uses this statement? :laugh:
Sergey Alexandrovich Kryukov 13-Aug-15 9:21am    
Thank you, Maciej.
—SA
bernova 13-Aug-15 4:46am    
the solution that use "ref" ,that find in stackoverflow that be accept solution.

my code is run , but if use in function don't return data on datagridview
Andy Lanng 13-Aug-15 5:29am    
You may see VB using ref more often. It does need to be implicitly stated in VB, but rarely ever in C#. I don't think I've used it in the 7 years I've worked with C#
Clarification: I see ByRef in VB (perhaps older versions). I think there are different limitations of parameter scope between VB and C#. I have never needed to use ref myself because there always seems to be another way to perform the same operation. This may not hold true forever ^_^
Sergey Alexandrovich Kryukov 13-Aug-15 9:22am    
The problem is not "ref" itself, but "ref" for "already" reference-type parameters. With value parameters it has the purpose.
—SA
In addition to solution 1 by Sergey Alexandrovich Kryukov[^] i would like to provide one general note:
You should work on data, not on UI components!


Imagine, you have a list of persons. The data are displayed in a datagridview (dgv). If you change the color of person's eyes, what you should do? Change the value of cell in dgv? No! A change must be made on Person object to be able to reflect changes on dgv!

Do you see the difference?
 
Share this answer
 
v2
Comments
bernova 13-Aug-15 5:22am    
Indeed when change datagridview on function and call function , don't return data on datagridview , but in the normal state , code does run.

what's related to data ?????!!!!! are you ok???
Maciej Los 13-Aug-15 6:13am    
Thank you, i'm fine.
Sergey Alexandrovich Kryukov 13-Aug-15 16:52pm    
Good point, in bold red, a 5. :-)
—SA
Maciej Los 15-Aug-15 11:07am    
Thank you, Sergey.
This solution applies to ASP.NET and is not applicable to this question

Please pay attention to the solution from Sergey Alexandrovich Kryukov regarding the terminology. It does help get questions answered a lot quicker.

Also, Maciej Los makes an excellent point regarding how you should handle data changes in your applications. Making sure you use objects and data as they are intended to be used will make your life soo much easier in the long run.

Neither of these solutions answer your question, But I suggest accepting them as well as this solution as they are valid points regarding your question.


Control events occur AFTER the page has been redrawn. If you change data at this stage then it is too late to have that data change shown on the page.

A simple way to get around this is to wrap your control in an UpdatePanel. This allows you to fire an event BEFORE the page is redrawn. ALSO it has the added bonus of being a partial-postback so the whole page doesn't have to reload every time.

There are more elegant solutions, but this is the simplest:

XML
<asp:Button runat="server" ID="btnButton1" Text="button"/>
<asp:UpdatePanel runat="server" ID="upGrid" ChildrenAsTriggers="False" UpdateMode="Conditional">
    <ContentTemplate>
        <div>
        <my grid and stuff to be updated/>
            </div>
    </ContentTemplate>
    <Triggers>
       <asp:AsyncPostBackTrigger ControlID="btnButton1" EventName="Click"/>
    </Triggers>
</asp:UpdatePanel>


What this does is pretty cool:
1: The page is posted back to the server using an AJAX method
2: Your event runs (the button click)
3: You call upGrid.Update() from the server (only if you don't specify the trigger, though)
4: The server renders the content template with the new data
5: The server response to the AJAX with the new html
6: The client rewrites the contenttemplate with the new HTML

Try it. Let me know if you get stuck :D
 
Share this answer
 
v4
Comments
Maciej Los 13-Aug-15 6:12am    
Andy, i'm not sure that OP is talking about ASP.NET gridview. He is using DataGridView, which points me out on Windows.Forms.DataGridView. But... i might be wrong...
Andy Lanng 13-Aug-15 6:17am    
You make a good point. If it is ASP then the issue makes sense, otherwise I don't know why the grid doesn't update :S

OP (bernova): Is this ASP or winforms?
bernova 13-Aug-15 8:48am    
thanks but i'm using desktop application
Andy Lanng 13-Aug-15 8:50am    
Ah - I'll leave then :S

good luck ^_^
finally , my problem has solve.

just I needed copy following code from the last of code to
C#
datagrid.DataSource = dt_naghd

under below code.
C#
dt_naghd.AcceptChanges();


albert: every things should be simple but not simpler
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900