DataGrid use Experience (call and connect database, etc.) _ Practical Tips

Source: Internet
Author: User
Tags oracleconnection

A small problem that turns negative numbers into 0 when you're working on the background database data that is bound in the DataGrid to the user is now recorded.

The data in the example shows this:

-------------------------------------------------------Split Line--------------------------------------
Call to 1.DataGrid

The DataGrid is a control that is not found in the VS Toolbox for ASP.net development, so calling it requires handwriting code:

Copy Code code as follows:

<asp:datagrid runat= "Server" cssclass= "DataList" autogeneratecolumns= "False" id= "Dgdata" ></asp:datagrid >

Note: AutoGenerateColumns whether the DataGrid is required to automatically generate the column meaning, true to allow generation, false to No

If we choose not to, as the example does, we need to specify the columns of the DataGrid ourselves, and the code expands to:

Copy Code code as follows:

<asp:datagrid id= "Dgdata" runat= "Server" autogeneratecolumns= "false" >
<Columns>
<asp:boundcolumn datafield= "LARGE" headertext= "Da" ></asp:BoundColumn>
<asp:boundcolumn datafield= "Smalls" headertext= "small" ></asp:BoundColumn>
</Columns>
</asp:DataGrid>

The result of this binding is:

If you are automatically expanding columns:

PS: What if we write this?

Copy Code code as follows:

<asp:datagrid id= "Dgdata" runat= "Server" autogeneratecolumns= "true" >
<Columns>
<asp:boundcolumn datafield= "LARGE" headertext= "Da" ></asp:BoundColumn>
<asp:boundcolumn datafield= "Smalls" headertext= "small" ></asp:BoundColumn>
</Columns>
</asp:DataGrid>

Choose True for the properties of the auto-curried column, and I'll add a custom column to it, and the result is

2. Connect to the database for operation

Copy Code code as follows:

OracleConnection conn = new OracleConnection ("Data source=xxx; User id=xxx; password=xxx; ");
String sqlcmd = "SELECT * from test_123";
Conn. Open ();
OracleCommand cmd = new OracleCommand (sqlcmd,conn);
DataSet Dsret = new DataSet ();
OracleDataAdapter ad = new OracleDataAdapter (cmd);
Ad. Fill (Dsret);
Conn. Close ();
int i = dsret.tables[0]. Rows.Count;
Int j = Dsret.tables[0]. Columns.count;

for (int k = 0; k < i; k++)
for (int m = 0; m < J; m++)
{
if (int. Parse (Dsret.tables[0]. ROWS[K][M]. ToString ()) < 0)
Dsret.tables[0]. ROWS[K][M] = "0";

}


Dgdata.datasource = Dsret.tables[0];
Dgdata.databind ();

The first is the Sanbang that connects the database
Create connection and query strings and Mount Commond,dataadapter fills.

I use a dataset as a large container, we can interpret the dataset as a closet, this cabinet has a lot of drawers, drawer is a DataTable, this drawer is a lattice, the box inside the things that we query out of the data, a lattice a number of children. Finding these numbers requires row and column coordinates to determine.

In general, if you do not specify which drawer the datasheet is in, the system defaults to the No. 0.

Since a DataTable is a class, its instance is an object, and the object inside it is an object, so you can't think that the values stored in the DataTable look like int, then my dsret.tables[0]. ROWS[K][M] can be used as an integral type, requiring conversion operations.

This will achieve the purpose I originally wanted. Very basic, do not laugh, I also learn while summing up, so that they can remember

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.