Scenario Description:
In the process of information change, such as planned changes and changes to the basic information of the form, we hope to show in a list whether the row data is added or deleted, or modified. In this article, information changes are stored in Table A, and the changed data is stored in table B. If you do not think about it carefully, we usually take out the data of two tables, each of which has a row record to compare with all the records of the other table, to determine whether the record is added, deleted, or modified. The following describes a simple method.
SQL description:
Code
Select status = (
Case
When N. nodeid is null then 'added'
When cmd. nodeid is null then 'deleted'
Else 'modified'
End
),
Tdname = (
Case
When cmd. tdname is null then n. tdname
Else cmd. tdname
End
)
From
(
Select cmd. nodeid,
Cmd. tdname
From table B cmd
Where query Conditions
) Cmd
Full join
(
Select N. nodeid, N. tdname
From table A N
Where query Conditions
) N
On cmd. nodeid = n. nodeid
The key is to pay attention to the meaning of the pseudo column status and why full join is used.
Processing in backend CS files:
Code
Protected void gvchangeprojectlist_rowdatabound (Object sender, gridviewroweventargs E)
{
If (E. Row. rowtype = datacontrolrowtype. datarow)
{
Datarowview DRV = E. Row. dataitem as datarowview;
Int columns = DRV. dataview. Table. Columns. count;
If (DRV ["status"]. tostring () = "added ")
{
E. Row. Attributes. Add ("title", "new ");
E. Row. backcolor = color. fromargb (0x66, 0xcc, 0xff );
}
Else if (DRV ["status"]. tostring () = "deleted ")
{
E. Row. Attributes. Add ("title", "delete ");
E. Row. backcolor = color. fromargb (0xff, 0x66, 0x33 );
}
Else
{
For (INT I = the first column to be compared; I <The last column to be compared; I ++)
{
Tablecell = E. Row. cells [I-1]; // The column in the data source is 1 larger than the column number in the list.
Bool equal = false;
Int org = I + the number of columns to be compared;
If (DRV [I] = dbnull. Value & DRV [org] = dbnull. value)
{
Equal = true;
}
Else if (DRV [I] = dbnull. Value | DRV [org] = dbnull. value)
{
Equal = false;
}
Else
{
If (DRV [I]. GetType () = typeof (string ))
{
Equal = (string) (DRV [I]) = (string) (DRV [org]);
}
Else if (DRV [I]. GetType () = typeof (short) | DRV [I]. GetType () = typeof (INT ))
{
Equal = (DRV [I]. tostring (). Trim () = DRV [org]. tostring (). Trim ());
}
Else if (DRV [I]. GetType () = typeof (datetime ))
{
Equal = (datetime) (DRV [I]). tostring ("yyyy-mm-dd") = (datetime) (DRV [org]). tostring ("yyyy-mm-dd ");
}
Else if (DRV [I]. GetType () = typeof (bool ))
{
Equal = (bool) (DRV [I]) = (bool) (DRV [org]);
}
}
If (! Equal)
{
String result = string. empty;
If (DRV [org]. GetType () = typeof (datetime ))
{
Result = string. Format ("data before modification: {0}", DRV [org] = dbnull. value? String. Empty: (datetime) DRV [org]). tostring ("yyyy-mm-dd "));
}
Else
{
Result = string. Format ("data before modification: {0}", DRV [org]);
}
Tablecell. tooltip = result;
Tablecell. backcolor = color. Yellow;
}
}
}
}
}
Lastbeachhead