简体   繁体   中英

How to call this method?

i'm just a newbie in programming and i made a function, just one stupid problem and a very stupid question.Please don't rude, How do i call this function from a different form or class. or even in the same form

public void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
            for (int i = 0; i <= dtInfo.Rows.Count - 2;i++ )
            {
                Battery = Convert.ToDateTime(dtInfo.Rows[i].Cells[5].Value.ToString());
                Oil = Convert.ToDateTime(dtInfo.Rows[i].Cells[14].Value.ToString());
                Fran = Convert.ToDateTime(dtInfo.Rows[i].Cells[12].Value.ToString());
                lastkm = int.Parse(dtInfo.Rows[i].Cells[13].Value.ToString());

            batt = Battery - DateTime.Now;
            doil = Oil - DateTime.Now;
            dfran = Fran - DateTime.Now;

            if (batt.Days <= 7)
            {
               dtInfo.Rows[i].Cells[5].Style.BackColor = Color.Green;

            }
            if (doil.Days <= 7)
            {
                dtInfo.Rows[i].Cells[14].Style.BackColor = Color.Green;
            }
            if (dfran.Days <= 7)
            {
                dtInfo.Rows[i].Cells[12].Style.BackColor = Color.Green;

            }
            if (lastkm <= 500)
            {
                dtInfo.Rows[i].Cells[13].Style.BackColor = Color.Green;
            }



        }
    }

EDITED

第一次公开表格 when i first open my form the color change then when i reopen it 重新打开表格 it wont change but when i trace the code the value of the color was change but not the color in the cell

That's an event handler, and you can't raise the event yourself unless:

  1. The class exposes a protected (or even public) method to fire it directly, but that doesn't happen usually in the .NET classes.

  2. You trigger it by doing what the actual event represents -- In your case, when the databinding between your grid and your source is complete.

You can call that method, though, but it wouldn't have any relevant meaning, since you wouldn't raise the event. Still, if the event logic doesn't matter and you just want that code to execute, you can do it through:

dataGridView1_DataBindingComplete(null, null);

But in that case, you can just wrap that method's content in a simple method with a returning type of void and no parameters.

dataGridView1_DataBindingComplete() is just a plain old method

dataGridView1_DataBindingComplete(this, new RoutedEventArs());

Should do the trick if you want to call it from the same object.

if you want to reuse that section of code I would take everything within the method and create a separate public method so you can call it from other places and from other objects.

It all depends where the function is located. If you have it under the same class as your form, you can just call it in the same scope. But from what I see, this is a event handler, and you shouldn't need to call it, as it handles the dataGridView event "DataBindingComplete". For more info about it, visit this .

However if you need to call it for some reason you can just do:

dataGridView1_DataBindingComplete(null, null)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM