Introduction: In this article we'll see how to format chemical formulae in DataGrid Web Server Control. In short display the chemical formulae stored in the database as CO2 to CO2 on the Webform.
The Database Data is stored as:To begin with User Interface Drag and drop
<
C#private void Page_Load(object sender, System.EventArgs e){// Put user code to initialize the page hereif (!Page.IsPostBack ){BindData ();}}DataSet ds = new DataSet ();void BindData(){string sqlStmt = "Select * from ChemicalFormula ";string conString = "server=localhost;database=Northwind;uid=sa;pwd=;";SqlDataAdapter myda = new SqlDataAdapter(sqlStmt, conString);myda.Fill(ds);DataGrid1.DataSource = ds;DataGrid1.DataBind();}protected string GetProperFormat(string fldval){string strval="" ; //Navigate through every character of the fldvalfor(int i = 0;i< fldval.Length ;i++){ //Check for IF the value is numeric True/Falseif(Char.IsNumber (fldval[i] )){strval = strval + "<sub>" + fldval[i] + "</sub>";}else{strval = strval + fldval[i];} }return strval;} VB.NETPrivate ds As DataSet = New DataSetPrivate Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load()'Put user code to initialize the page hereIf Not Page.IsPostBack Thenbinddata()End IfEnd SubSub binddata()Dim sqlStmt As String = "Select * from ChemicalFormula "Dim conString As String = "server=localhost;database=Northwind;uid=sa;pwd=;"Dim myda As SqlDataAdapter = New SqlDataAdapter(sqlStmt, conString)myda.Fill(ds)DataGrid1.DataSource = dsDataGrid1.DataBind()End SubFunction GetProperFormat(ByVal fldval As String)Dim FieldsValues() As String = Regex.Split(fldval, "")Dim i As IntegerDim strval As String'Navigate through every character of the FieldValueFor i = 0 To FieldsValues.Length - 1'Check for IF the value is numeric True/FalseIf IsNumeric(FieldsValues(i)) Thenstrval = strval & "<sub>" & FieldsValues(i) & "</sub>"Elsestrval = strval & FieldsValues(i)End IfNextReturn strvalEnd Function
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: