【问题标题】:How can remove/replace substring from GridView Column?如何从 GridView 列中删除/替换子字符串?
【发布时间】:2015-10-27 06:27:41
【问题描述】:

我有下面的 GridView:

我想从日期列中删除0:00:00(如果存在)。我更改了dataformatstring,但也删除了9:09:15

我在.cs 文件上使用auto-generate fieldsDataBind

我试过Replace,但还是不行:

e.Row.Cells[0].Text = e.Row.Cells[0].Text.Replace("0:00:00"," ");

我该怎么做?

网格视图:

<asp:GridView ID="GridView4" runat="server" BackColor="White" 
    BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
    GridLines="Vertical" Width="98%" 
    Height="100%" onrowdatabound="GridView4_RowDataBound" RowStyle-HorizontalAlign="Center" >
    <AlternatingRowStyle BackColor="#DCDCDC" />
    <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
    <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
    <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
    <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#0000A9" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>

代码:

protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            OleDbConnection Connection6;
            using (Connection6 = new OleDbConnection("Provider=MSDAORA.1;Data Source=XXXXXX:1521/orcl;Persist Security Info=True;Password=XXXXXXX;User ID=XXXXXX;"))
            {
            string sqlQuery = "select * from db.user";

            using (OleDbDataAdapter cmd = new OleDbDataAdapter(sqlQuery, Connection6))
            {
                Connection6.Open();
                DataTable dt = new DataTable();
                cmd.Fill(dt);
                GridView4.DataSource = dt;
                GridView4.DataBind();

                Connection6.Close();
            }
        }
    }
        catch (Exception)
        {
        }
    }

【问题讨论】:

  • that 列的类型是什么?例如,您可以将其DataFormatString 属性设置为{0:dd.MM.yyyy}
  • 您在哪里提供此代码e.Row.Cells[0].Text = e.Row.Cells[0].Text.Replace("0:00:00"," "); 如果不是,我希望您在数据绑定中使用相同的代码
  • GridView 中的列部分在哪里?

标签: c# asp.net .net gridview


【解决方案1】:

你可以使用 RowDataBound 事件,然后使用 findcontrol 标签控件

<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server"
        onrowdatabound="GridView1_RowDataBound">
  <Columns>    
    <asp:TemplateField HeaderText="Date">
      <ItemTemplate>
         <asp:Label ID="lblDate" Text='<%# Eval("Date") %>' runat="server"></asp:Label>
 .....
 </Columns>
</asp:GridView>

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
   {      
    var lbl =  e.Row.FindControl("lblDate") as Label;
    lbl.Text = lbl.Text.Replace("0:00:00", "");     
   }
}

如果您没有模板字段并使用绑定字段,则使用

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
    if(e.Row.Cells[0].Text.Contains("0:00:00"))
       e.Row.Cells[0].Text = e.Row.Cells[0].Text.Replace("0:00:00", "");
  }
}

【讨论】:

  • 对不起,我没这么说,我用auto-generate fields
  • @Rahul Singh:- OP 只想删除 0:00:00,如果存在则不需要采取任何行动,你觉得有点不同吗?
  • @HaveNoDisplayName - 哎呀我的错。是的,没看到。
  • @RahulSingh:- np,没关系
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-16
  • 2021-07-28
  • 2017-07-26
  • 2014-02-24
  • 1970-01-01
  • 2021-06-24
相关资源
最近更新 更多