关于.NET Gridview 部分运用分享

独立按钮获取GridView 修改/删除 ID

protected void ImageButton_Click(object sender, EventArgs e)
{

//根据获取的行数获取指定ID
int rowIndex = ((GridViewRow) (((ImageButton)sender).NamingContainer)).RowIndex;
string ID = GridView.DataKeys[rowIndex].Value.ToString();

}

序号列

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//1开始逐行递增
e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
}
}

后台指定行显示内容

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
switch (e.Row.Cells[1].Text.ToString())
{
case “1”:
e.Row.Cells[1].Text = “自定义1”;
break;
case “2”:
e.Row.Cells[1].Text = “自定义2”;
break;
}
}
}

前台根据判断显示内容

<asp:Label ID=“type” runat=“server”
Text=’<%# (Eval(“STID”).ToString()==“0”?“系统模板”:“自定义模板”) %>’></asp:Label>

双击事件,可用于双击打开详细信息/调用前台函数交互

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
e.Row.Attributes.Add(“ondblclick”, “winReg();”)
;//双击,需调用后台函数
}
}

GridView 表头/行内 内容过多不换行

protected void GridView_RowCreated(Object sender, GridViewRowEventArgs e)
{

GridUser.HeaderStyle.Wrap = false;//表头不允许换行
GridUser.RowStyle.Wrap = false;//表内容不允许换行

if (e.Row.RowType == DataControlRowType.DataRow)
{
   for (int i = 0; i < e.Row.Cells.Count; i++)
  {
   e.Row.Cells[i].Attributes.Add("style", "word-break :keep-all ; word-wrap:keep-all");
  }   
}

}

数据绑定后怼Datatable进行sql再次筛选

DataTable newdt = new DataTable();
newdt = dt.Clone(); // 克隆dt 的结构,包括所有 dt 架构和约束,并无数据;
DataRow[] rows = dt.Select(“manyiFlag not in (‘满意’,‘非常满意’,‘对客服不满意’,‘对其他不满意’) and manyiFlag in (‘满意’,‘非常满意’,‘对客 服不满意’,‘对其他不满意’)”); // 从dt 中查询符合条件的记录;
foreach (DataRow row in rows) // 将查询的结果添加到dt中;
{
newdt.Rows.Add(row.ItemArray);
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值