DataGrid专题

1 Grid里日期格式化
 数据段格式:{0:yyyy-M-d}
<asp:BoundColumn DataField="Time" ReadOnly="True" HeaderText="时间" DataFormatString="{0:yyyy-M-d}"></asp:BoundColumn>
{0:d}


2 参数链接列(举例)
URL字段:ID
URL格式字符串:managerplandetail.aspx?id={0}
文本:查看(或&lt;img src=&quot;../images/showdetails.gif&quot;&gt;)


3 按钮列(DeleteCommand,EditCommand)
a.id = AddDG.DataKeys(e.Item.ItemIndex)
a.delete()
注意:需要为DataGrid设置DataKeyField,如ID

4 为特定行加“确认删除”
e.Item.Cells[10].Attributes.Add("onclick", "return confirm('确定要删除id="+id+"吗?');");//C#

datagrid-》属性生成器-》列-》添加按钮列-》删除-》文本(T)->在文本框里加上:
<div id="de" οnclick="JavaScript:return confirm('确定删除吗?')">删除</div>

 


5 当Datasource没有数据时,显示“暂无数据”的方法
'第一种方法
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string SOPName=Convert.ToString(DataBinder.Eval(e.Item.DataItem,"需要处理的列名"));
if(SOPName=="")
{
e.Item.Cells[3].Text="暂无数据";
}
}
'第二种方法
假如数据库里面的表table1的结构如下:
create table table1
(
   sid int identity(1,1),
   username varchar(10),
   pwd varchar(20)
)

insert into table1
select 'aa','123'
union all select 'bb','123'
union all select 'cc',''
union all select 'dd',''
union all select 'ee','123'

SQL语句这样写:
select username,case when isnull(convert(varchar(20),pwd),'')='' then '暂无数据' else
convert(varchar(20),pwd) end pwd from table1

'第三种方法
DataGrid1.DataSource=dv;
if(dv.Count==0)//dv为dataview
{
lbl_noResult.Visible=true;//这个label上写着“暂无记录”
DataGrid1.Visible=false;//datagrid不可见
}
else
{
lbl_noResult.Visible=false;
DataGrid1.Visible=true;//有数据,datagrid可见

}
DataGrid1.DataBind();

}

6 动态为DataGrid更改列标题
 Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
        Dim i As Integer
        If e.Item.ItemType = ListItemType.Header Then
            For i = 0 To e.Item.Cells.Count - 1
                If e.Item.Cells(i).Text = "CS" Then
                    e.Item.Cells(i).Text = "平均车速"
                ElseIf e.Item.Cells(i).Text = "CSL" Then
                    e.Item.Cells(i).Text = "车数"
                End If
            Next
        End If
  '设置某列宽度
  DataGrid1.Items(0).Cells(0).Style("width") = "80px"

'自动判定时间,警告颜色
  Private Sub DGPlans_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DGPlans.ItemDataBound
        Dim span As TimeSpan
        Try
            span = Convert.ToDateTime(e.Item.Cells(6).Text).Subtract(Convert.ToDateTime(Now.ToShortDateString))
            If span.Days < 5 Then '结束时间还有五天
                e.Item.BackColor = Color.GreenYellow
            End If
            If span.Days = 1 Then '结束时间还有一天
                e.Item.BackColor = Color.Yellow
            End If
            If span.Days = 0 Then '已经到了结束时间
                e.Item.BackColor = Color.Red
            End If
            If span.Days < 0 Then '已经超出了结束时间
                e.Item.BackColor = Color.Black
                e.Item.ForeColor = Color.White
            End If
        Catch
        End Try
    End Sub


'选中变色及还原
Private Sub DGDaily_EditCommand
clearcolor()
  If e.Item.Cells(8).Text = "Y" Then
            e.Item.BackColor = Color.Gold
            e.Item.Cells(8).Text = "N"
        End If

        e.Item.BackColor = Color.Goldenrod
        e.Item.Cells(8).Text = "Y"


Public Sub clearcolor()
        Dim i As Integer
        For i = 0 To DGDaily.Items.Count - 1

            If DGDaily.Items.Item(i).Cells(8).Text = "Y" Then
                DGDaily.Items.Item(i).BackColor = Color.Gold
            End If
        Next
  End Sub

'排序及分页
外:'
  AddDG.DataSource = address.list(Trim("Add_" + e.SortExpression))'目的是把排序表达式变为字段名

list():
comstr = "select Add_ID,Add_name,Add_Mobile,Add_Home,Add_type from address order by " + sortstr

            If sor = " desc" Then
                sor = " asc"
            Else
                sor = " desc"
            End If
            comstr = comstr + sor
            Dim com As New SqlDataAdapter(comstr, con)


排序:

DGPlans.CurrentPageIndex = e.NewPageIndex
        BindDG() 

7 动态翻译表头及设定内容格式

        Dim i As Integer
        '头部分
        If e.Item.ItemType = ListItemType.Header Then

            Formats = New String(e.Item.Cells.Count - 1) {}
            For i = 0 To e.Item.Cells.Count - 1
                Formats(i) = "M"
            Next
            For i = 0 To e.Item.Cells.Count - 1
                If e.Item.Cells(i).Text = "CS" Then
                    Formats(i) = "0.00"
                    e.Item.Cells(i).Text = "平均车速"
                ElseIf e.Item.Cells(i).Text = "LB" Then
                    e.Item.Cells(i).Text = "类别"
                    Formats(i) = "T"
                End If
            Next
            '内容部分
        ElseIf e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim j As Integer
            For j = 0 To e.Item.Cells.Count - 1
                If Formats(j) = "T" Then
                    If Trim(e.Item.Cells(j).Text) = "J" Then e.Item.Cells(j).Text = "购进"
                    If Trim(e.Item.Cells(j).Text) = "C" Then e.Item.Cells(j).Text = "售出"
                End If
                If Formats(j) = "0.00" Then
                    e.Item.Cells(j).Text = CType(e.Item.Cells(j).Text, Double).ToString(Formats(j))
                End If
            Next
        End If

8 DataGrid行随鼠标变色
private void DGzf_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
 if (e.Item.ItemType!=ListItemType.Header)
 {
  e.Item.Attributes.Add( "onmouseout","this.style.backgroundColor=/""+e.Item.Style["BACKGROUND-COLOR"]+"/"");
  e.Item.Attributes.Add( "onmouseover","this.style.backgroundColor=/""+ "#EFF3F7"+"/"");
 }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值