c# 化学式表示

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

  • DataGrid

<

asp:DataGrid id ="DataGrid1" AutogenerateColumns ="false" runat ="server">
<
Columns >
<
asp:TemplateColumn HeaderText ="Chemical Formula">
<
ItemTemplate >
<
font face =Arial size =3 >
<
b >
<%#GetProperFormat(DataBinder.Eval(Container.DataItem,"Formula").ToString())%>
</ b >
</
font >
</
ItemTemplate >
</
asp:TemplateColumn >
</
Columns >
</
asp:DataGrid >

 

Now our code. :
  • Method BindData : To Populate the DataSet and assign the value to relevant control
  • Function GetProperFormat : To Format Chemical Formula as H2O(storedin database) to H2O(on the WebForm).

C#

private
void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!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 fldval
for(int i = 0;i< fldval.Length ;i++)
{
//Check for IF the value is numeric True/False
if(Char.IsNumber (fldval[i] ))
{
strval = strval + "<sub>" + fldval[i] + "</sub>";
}
else
{
strval = strval + fldval[i];
}
}
return strval;


VB.NET

Private ds As DataSet = New DataSet
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load()
'Put user code to initialize the page here
If Not Page.IsPostBack Then
binddata()
End If
End
Sub
Sub
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 = ds
DataGrid1.DataBind()
End Sub
Function
GetProperFormat(ByVal fldval As String)
Dim FieldsValues() As String = Regex.Split(fldval, "")
Dim i As Integer
Dim strval As String
'Navigate through every character of the FieldValue
For i = 0 To FieldsValues.Length - 1
'Check for IF the value is numeric True/False
If IsNumeric(FieldsValues(i)) Then
strval = strval & "<sub>" & FieldsValues(i) & "</sub>"
Else
strval = strval & FieldsValues(i)
End If
Next
Return strval
End Function

The Output of this code:

转载于:https://www.cnblogs.com/chenqi/archive/2010/12/16/1907404.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值