【问题标题】:ASP.NET databound string column formatASP.NET 数据绑定字符串列格式
【发布时间】:2021-12-05 12:02:48
【问题描述】:

在我的 Web 应用程序中,我有一个 gridview,其中有来自数据库的数据。它看起来像这样:

number place
1234579 home2
1787543 home1

我想编辑第一列 (number) 以格式化 - 右侧的第五个字符加粗,右侧的三个字符也加粗。

number place
1234579 home2
1787543 home1

我怎样才能做到这一点?我在视觉工作室工作,它可以在设计模式或源代码中设置,或者如何设置。谢谢

【问题讨论】:

    标签: c# asp.net visual-studio


    【解决方案1】:

    您可以使用TemplateField 将数据移动到代码后面并根据需要进行格式化 - 因为在这里您必须拆分您的号码,然后重新格式化。

    所以在 GridView 里面添加一个<asp:TemplateField>

    <Columns>
        <asp:TemplateField HeaderText="number" >
            <ItemTemplate>
                <%#RenderLine(Container.DataItem)%>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    

    在代码后面做你的格式..

    protected string RenderLine(object oItem)
    {        
        var yourNumber = DataBinder.Eval(oItem, "fieldname").ToString();
    
         // split yourNumber, then reformat it using <b> etc... and return it
         // need to do some work here, here is an example
         var newNumberFormat = string.Format("<b>{0}</b>", yourNumber);
    
         return newNumberFormat;
    }
    

    【讨论】:

    • 感谢您指出正确的方向。就在我尝试的时候,它不是列中的数据,而是在每一行显示System.Data.DataRowView
    • @Cesc 使用此DataBinder.Eval(oItem, "fieldname").ToString(); 从您的字段中获取数据
    【解决方案2】:

    假设源是某种数据库,所以底线是您必须提取该数据并使用代码对其进行格式化。

    我的意思是,将整个列加粗很简单。但这有点麻烦。

    好的,所以我们在表格中说这些数据:

    我们的标记是这样的:

     <asp:GridView ID="GridView1" runat="server" CssClass="table"></asp:GridView>
    

    好的,我们加载网格的代码是这样的:

        void LoadGrid()
        {
            using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
            {
                using (SqlCommand cmdSQL = new SqlCommand("SELECT SNumber, Place from tblJunk", conn))
                {
                    conn.Open();
                    GridView1.DataSource = cmdSQL.ExecuteReader();
                    GridView1.DataBind();
                }
            }
        }
    

    我们现在有了这个:

    现在,作为“一般”规则,格式化网格/列表视图等,我们可以尝试将消息服务器表达式注入到标记中,但是为了更好地设置颜色、突出显示和格式化,使用该控件的数据绑定事件。

    所以,我们可以有这样的代码:

       protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string txt = e.Row.Cells[0].Text;
    
                // bold 5th from right
                int rPos = txt.Length - 4;
                txt = MyBold(txt, rPos, 1);
    
                // bol last 3 chars
                rPos = txt.Length - 2;
                txt = MyBold(txt, rPos, 3);
                e.Row.Cells[0].Text = txt;
            }
        }
    

    现在我们的输出是这样的:

    因此,对于更改状态和格式化网格(例如状态颜色等),以上是一般方法。

    当然,我们需要一些名为 MyBold 的函数。

    这样说:

       string MyBold(string s,int iStart, int iEnd)
        {
            // ONE based postions - NOT 0 based!
            iStart -= 1;
    
            s = rLeft(s, iStart) + "<strong>" + s.Substring(iStart, iEnd) + "</strong>" +
                s.Substring(iStart + iEnd, s.Length - (iStart + iEnd));
            return s;
        }
    
        string rLeft(string s,int r)
        {
            return s.Substring(0, r);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-31
      • 1970-01-01
      • 2011-10-11
      • 2013-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多