BackgroundCode:
View code
1 Protected Void Page_load ( Object Sender, eventargs E)
2 {
3 If ( ! Ispostback)
4 {
5 Databind ();
6 }
7
8 }
9 Public Void Databind ()
10 {
11 Sqlconnection con = New Sqlconnection (configurationmanager. connectionstrings [ " Conn " ]. Tostring ());
12 Sqlcommand cmd = New Sqlcommand ();
13 Cmd. Connection = Con;
14 Cmd. commandtext = " Select * from MERs " ;
15 Sqldataadapter da = New Sqldataadapter (CMD );
16 Dataset DS = New Dataset ();
17 Da. Fill (DS );
18 This . Gridview1.datasource = DS. Tables [ 0 ];
19 This . Gridview1.datakeynames = New String [] { " Customerid " };
20 This . Gridview1.databind ();
21 Dataview View = DS. Tables [ 0 ]. Defaultview;
22 For ( Int I = 0 ; I < This . Gridview1.rows. Count; I ++ )
23 {
24 If ( This . Gridview1.pageindex = 0 )
25 {
26 Datarowview rowview = View [I];
27 String Str = Rowview [ " CompanyName " ]. Tostring ();
28 This . Gridview1.rows [I]. cells [ 1 ]. Text = Substringstr (STR, 4 );
29 }
30 Else
31 {
32 Datarowview rowview = View [I + ( This . Gridview1.pageindex * This . Gridview1.pagesize)];
33 String Str = Rowview [ " CompanyName " ]. Tostring ();
34 This . Gridview1.rows [I]. cells [ 1 ]. Text = Substringstr (STR, 4 );
35 }
36
37 }
38 }
39 Public String Substringstr ( String STR, Int Len)
40 {
41 If (Str. Length <= Len)
42 {
43 Return STR;
44 }
45 Else
46 {
47 Return Str. substring ( 0 , Len) + " ... " ;
48 }
49 }