CType據說是VB非常好用的強類型轉換,剛好我要寫序號正想拿來用....才發現C#沒有...
好吧,其實有啦,就是要轉一下寫法~
其實還蠻方便的
參考網址、參考網址、參考網址
前台....話說如果要流水號的話,不考慮分頁其實第一個欄位那種表示方法最快~
<asp:GridView ID="gvData" runat="server" AutoGenerateColumns="False"
onrowdatabound="gvData_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="流水號">
<ItemTemplate>
<%#Container.DataItemIndex + 1%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="序號">
<ItemTemplate>
<asp:Label ID="lblNo" runat="server" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="資料名稱" DataField="cType" />
</Columns>
</asp:GridView>
後台程式如下,紅色部分就是CType在C#的寫法,轉換規則如下
vb: CType(Sender,Control)
C#:(Control)Sender
protected void Page_Load(object sender, EventArgs e)
{
gvData.DataSource = TmpData();
gvData.DataBind();
}
protected void gvData_RowDataBound(object sender, GridViewRowEventArgs e)
{
Label clabel = new Label();
GridView cgridview = new GridView();
if (e.Row.RowType == DataControlRowType.DataRow)
{
clabel = (Label)e.Row.Cells[0].FindControl("lblNo");
cgridview = (GridView)sender;
clabel.Text = (cgridview.PageIndex * cgridview.PageSize) + (e.Row.RowIndex + 1).ToString();
}
}
private DataTable TmpData()
{
DataTable dtr = new DataTable();
dtr.Columns.Add("cType");
dtr.Rows.Add(new object[] { "Aries" });
dtr.Rows.Add(new object[] { "Taurus" });
dtr.Rows.Add(new object[] { "Gemini" });
dtr.Rows.Add(new object[] { "Cancer" });
dtr.Rows.Add(new object[] { "Leo" });
dtr.Rows.Add(new object[] { "Virgo" });
dtr.Rows.Add(new object[] { "Libra" });
dtr.Rows.Add(new object[] { "Scorpio" });
dtr.Rows.Add(new object[] { "Sagittarius" });
dtr.Rows.Add(new object[] { "Capricorn" });
dtr.Rows.Add(new object[] { "Aquarius" });
dtr.Rows.Add(new object[] { "Pisces" });
return dtr;
}
沒有留言:
張貼留言