简体   繁体   中英

I need a row Added event for a DataGridView

What i want to do is set the background of a row based on some criteria, but the datagrid will be fairly large so i don't want to have to loop over all the rows again. The rows get created me doing something like "myDataGridView.DataSource = MyDataSource, so the only way i can think to edit rows is by using an event.

there is a row s added event, but that gives me a list of rows that i'd have to iterate over.

Thanks in advance for any help.

So where is your problem?

You have to put your finger on each row for a minimum of one time. So what is the difference if you get in a RowAdded event a single row or a bunch of them? In the case of a bunch you also get every line only one time, but with lesser events, which could also result in a faster code (but to be sure i would do profiling on that!).

While this may not answer your question directly, did you consider assigning the datasource while it is still empty, and then populate the datasource with data. In this case, if for example you are using a binding list, you can make use of the event provided by the bindinglist to determine if a new item was added or not

I'm using the RowStateChanged event now. It fires more often then i need, but thats not really a problem. I found something really odd with this one. It looked like if set the background color of the first row, it changed all the rows. I can't figure out why it did that, but i just added an else statement to force everything else to be white.

Its good enough for me. Thanks for the help guys.

It's been a while, but I think I used the DataGridView.RowPostPaint event in a few situations where I needed just that.

From the event args passed into the event handler, you have access to the RowIndex, allowing you to get to the Row object, from which you can reference the DataBoundItem etc.

Please look it up and see if it does what you need.

HTH

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