绕过datahridview作为参数来运行 [英] Bypass datahridview as parameter to function

查看:70
本文介绍了绕过datahridview作为参数来运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我想绕过datagridview来运行。但是当调用函数时,不返回datagridview上的数据

Hi all

I wanna bypass datagridview to function . but when call function , doesn't return data on datagridview

 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;
            }





并在button1_click()事件中调用函数:



and call function in button1_click() event:

 private void button1_Click(object sender, EventArgs e)
 {
  Select_all_options_insellGrid(Convert.ToInt32(factor_num.Text), ref dataGridView1);
}

推荐答案

首先,删除 ref 来自功能声明;即使你想修改这个对象,它也毫无意义。 DataGridView 引用类型,因此通过值传递的引用足以传递给方法,即使它通过修改对象也是如此参考。



我不确定不要开火是否有意义;即使你使用这样的行话,你也没有提及任何可能触发的事件或任何事情。如果其他东西不起作用,请先使用调试器。



现在,在进行任何编程之前,您确实需要学习参考类型,引用和对象。当然,你必须开始学习OOP,还有更多。在熟悉一般编程之前,我不会开发任何UI或任何其他高级主题,最好在简单的控制台应用程序上练习。



-SA
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


除了 Sergey Alexandrovich Kryukov [ ^ ]我想提供一个一般性说明:

In addition to solution 1 by Sergey Alexandrovich Kryukov[^] i would like to provide one general note:
你应该处理数据,不在UI组件上!





想象一下,你有一份人员名单。数据显示在datagridview(dgv)中。如果你改变了人眼的颜色,你应该怎么做?在dgv中更改单元格的值?没有!必须在 Person 对象上进行更改才能反映对dgv的更改!



你看到了区别吗?



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?


这个解决方案适用于ASP.NET和不适用于这个问题



请注意Sergey Alexandrovich Kryukov关于术语的解决方案。它确实有助于更快地回答问题。



此外,Maciej Los对于如何处理应用程序中的数据更改提出了一个很好的观点。确保您使用对象和数据,从长远来看,这将使您的生活更加轻松。



这些解决方案都没有回答您的问题,但我建议接受它们以及此解决方案,因为它们是关于您的问题的有效点。




控制事件发生在页面重绘之后。如果您在此阶段更改数据,则在页面上显示数据更改为时已晚。



解决此问题的一种简单方法是包装您的控件在UpdatePanel中。这允许您在重绘页面之前触发事件。此外它还有部分回发的额外好处,因此整个页面不必每次都重新加载。



还有更优雅的解决方案,但这是最简单的:



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:

<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>





这样做很酷:

1:使用AJAX方法将页面发回服务器

2:您的事件运行(按钮单击)

3:您调用upGrid来自服务器的.Update()(仅当你没有指定触发器时)

4 :服务器使用新数据呈现内容模板

5:服务器使用新html对AJAX的响应

6:客户端使用新HTML重写contenttemplate



试一试。如果你遇到困难,请告诉我:D



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


这篇关于绕过datahridview作为参数来运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆