Through the public function, the gridview can be merged into the rowspan (Row-join)

Source: Internet
Author: User
Tags connectionstrings

After using the gridview, you should have more or less such a requirement, that is, some duplicate rows may be used to combine rowspan, it should be easy to find such a solution on the Internet. But since it is something that may often be used, it is better to extract it into a public function. When it is not needed, it is better to call the function directly.

In the Order Details Example of our North Region data, select the first 50 shards, and then expect the same orderid to be combined.

The original form

After Contract

First, sort out common categories.

Imports Microsoft. visualbasicpublic class objpublic public sub gvrowspan (byval parent as system. web. UI. control, byval gridvewname as string, byval rowindex () as integer, byval selfcolor as Boolean, byval mycolors as system. drawing. color () dim mysinglerow as gridviewrow dim TMPS as string = "" 'is used to store rowspan text dim tmpi as integer' Used in rowspan's rowindex 'dim GV as gridview = ctype (Me. findcontrol (gridvewname), gridview) dim GV as gridview = ctype (Me. findcontrolex (parent, gridvewname), gridview) dim colorcnt as integer = mycolors. length dim coloridx as integer = 0 dim y as integer if GV isnot nothing then for each mysinglerow in GV. rows start from rowindex> 0 if mysinglerow. rowindex = 0 then TMPS = mysinglerow. cells (rowindex (0 )). text. trim () for Y = 0 to rowindex. count-1 mysinglerow. cells (rowindex (y )). rowspan = 1 next tmpi = 0 if selfcolor then mysinglerow. backcolor = mycolors (coloridx mod colorcnt) end if mysinglerow. rowindex> 0 then': determines whether the current data is consistent with the expected data. If mysinglerow. cells (rowindex (0 )). text. trim () = TMPS then' must be merged for Y = 0 to rowindex. count-1 'rowspan + 1 GV of the row of the merge. rows (tmpi ). cells (rowindex (y )). rowspan + = 1' indicates the merged row, visiable = false mysinglerow. cells (rowindex (y )). visible = false next if selfcolor then mysinglerow. backcolor = mycolors (coloridx mod colorcnt) end if else does not need to combine the content set for the next check TMPS = mysinglerow. cells (rowindex (0 )). text. trim () for Y = 0 to rowindex. count-1 'specifies the rowspan of the row = 1 mysinglerow. cells (rowindex (y )). rowspan = 1 next 'sets the index tmpi = mysinglerow. rowindex if selfcolor then coloridx + = 1 mysinglerow. backcolor = mycolors (coloridx mod colorcnt) end if next end if end sub public function findcontrolex (byval parent as system. web. UI. control, byval ID as string) as system. web. UI. control dim octrl as system. web. UI. control = nothing dim ochildctrl as system. web. UI. control = nothing 'first use findcontrol to locate the specified sub-control role octrl = parent. findcontrol (ID) 'If the cursor cannot be found, perform the following operations to locate () If octrl is nothing then for each ochildctrl in parent. controls 'Call the original function octrl = findcontrolex (ochildctrl, ID) in the form of commandid) 'If someone finds the specified control role, then the entire circle is opened. If octrl isnot nothing then exit for next end if return octrl end functionend class

then, the hidden area of the hidden area

<Div> <asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "false" cellpadding = "4" datakeynames = "orderid, productid "ceceid =" sqldatasource1 "emptydatatext =" No information is displayed. "Forecolor =" #333333 "gridlines =" NONE "> <rowstyle backcolor =" # e3eaeb "/> <columns> <asp: boundfield datafield = "orderid" headertext = "orderid" readonly = "true" sortexpression = "orderid"/> <asp: boundfield datafield = "productid" headertext = "productid" readonly = "true" sortexpression = "productid"/> <asp: boundfield datafield = "unitprice" headertext = "unitprice" sortexpression = "unitprice"/> <asp: boundfield datafield = "quantity" headertext = "quantity" sortexpression = "quantity"/> <asp: boundfield datafield = "discount" headertext = "discount" sortexpression = "discount"/> </columns> <footerstyle backcolor = "#1c5e55" font-bold = "true" forecolor =" white "/> <pagerstyle backcolor =" #666666 "forecolor =" white "horizontalalign =" center "/> <selectedrowstyle backcolor =" # c5bbaf "font-bold =" true "forecolor = "#333333"/> 

The method used is very simple. In the prerender event of the gridview, call the function of the class and adjust the parameter value.

 
Protected sub gridviewincluprerender (byval sender as object, byval e as system. eventargs) handles gridview1.prerender dim selfcolor as Boolean = true dim color1 as system. drawing. color = system. drawing. color. fromargb (216,216,255) dim color2 as system. drawing. color = system. drawing. color. fromargb (1, 255,251,158) dim colors (1) as system. drawing. color Colors (0) = color1 colors (1) = color2 dim opub as new objpublic dim rowindex (0) as integer rowindex (0) = 0 dim gridviewname as string = "gridview1" opub. gvrowspan (page, gridviewname, rowindex, selfcolor, colors) end sub

In this way, you can upload

Pai_^

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.