gridview整理(一)

====================编辑,删除,更新 

 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GridViewBind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        GridViewBind();
    }

 

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
        string card = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TBCard")).Text;
        try

       {

       ..................更新

 

            GridView1.EditIndex = -1;
            GridViewBind();
        }
        catch (Exception ex)
        {
            Response.Write("数据库错误,错误原因:" + ex.Message);
            Response.End();
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
        try

        {

 

             删除................

 

            GridView1.EditIndex = -1;
            GridViewBind();
        }
        catch (Exception ex)
        {
            Response.Write("数据库错误,错误原因:" + ex.Message);
            Response.End();
        }
    }
}

 

=============排序

 

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        { 
            ViewState["SortOrder"] = "身份证号码";
            ViewState["OrderDire"] = "ASC";

            bind();
        }
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sPage = e.SortExpression;
        if (ViewState["SortOrder"].ToString() == sPage)
        {
            if (ViewState["OrderDire"].ToString() == "Desc")
                ViewState["OrderDire"] = "ASC";
            else
                ViewState["OrderDire"] = "Desc";
        }
        else
        {
            ViewState["SortOrder"] = e.SortExpression;
        }
        bind();
    } http://www.mscto.com

 

    public void bind()
    {
       
        string sqlstr = "select top 5 * from 飞狐工作室";
        sqlcon = new SqlConnection(strCon);
        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
        DataSet myds = new DataSet();
        sqlcon.Open();
        myda.Fill(myds, "飞狐工作室");
        DataView view = myds.Tables["飞狐工作室"].DefaultView;
        string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
        view.Sort = sort;
        GridView1.DataSource = view;

 


        GridView1.DataBind();
        sqlcon.Close();
    }
}

 

 

======================GridView实现自定义时间货币等字符串格式:

在asp.net 2.0中,如果要在绑定列中显示比如日期格式等,如果用下面的方法是显示不了的

<asp :BoundField DataField="CreationDate"
DataFormatString="{0:M-dd-yyyy}"
HeaderText="CreationDate" />

主要是由于htmlencode属性默认设置为true,已防止XSS攻击,安全起见而用的,所以,可以有以下两种方法解决

http://www.mscto.com

 

1、 http://www.mscto.com

<asp :GridView ID="GridView1" runat="server">
<columns>
<asp :BoundField DataField="CreationDate"
DataFormatString="{0:M-dd-yyyy}"
HtmlEncode="false
"
HeaderText="CreationDate" />
</columns>
</asp>

 

 

将htmlencode设置为false即可

另外的解决方法为,使用模版列

<asp :GridView ID="GridView3" runat="server" >
<columns>
<asp :TemplateField HeaderText="CreationDate" >
<edititemtemplate>
<asp :Label ID="Label1" runat="server"
Text='<%# Eval("CreationDate", "{0:M-dd-yyyy}") %>'>
</asp>
</edititemtemplate>
<itemtemplate>
<asp :Label ID="Label1" runat="server"
Text=’<%# Bind("CreationDate", "{0:M-dd-yyyy}") %>'>
</asp>
</itemtemplate>
</asp>
</columns>
</asp>

{0:C} 货币;
{0:D4}由0填充的4个字符宽的字段中显示整数;
{0:000.0}四舍五入小数点保留第几位有效数字;
{0:N2}小数点保留2位有效数字;{0:N2}%   小数点保留2位有效数字加百分号;
{0:D}长日期;{0:d}短日期;{0:yy-MM-dd}   例如07-3-25;;{0:yyyy-MM-dd} 例如2007-3-25

 

===================GridView一般换行与强制换行

首先设置<asp:BoundField DataField="家庭住址" HeaderText="家庭住址" ItemStyle-Width="100" />
gridview里有一列绑定的数据很长,显示的时候在一行里面显示,页面拉得很宽。
原因是连续英文段为一个整体导致的,在RowDataBound中添加上了一句e.Row.Cells[2].Style.Add("word-break", "break-all")就可以。

 

 

如果要给所有的列增加此属性:
protected void Page_Load(object sender, EventArgs e)
    {
        //正常换行
        GridView1.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
        //下面这行是自动换行
        GridView1.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");

        if (!IsPostBack)
        {
              bind();//调用数据绑定即可
        }
    }
总之:善用CSS的word-break:break-all;word-wrap:break-word属性即可,这个属性是通用的对于顽固的难换行问题都可以解决,不局限于GridView。

 

============================ GridView突出显示某一单元格(例如金额低于多少,分数不及格等)
GridView1.DataBind();
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            DataRowView mydrv = myds.Tables["飞狐工作室"].DefaultView[i];
            string score = Convert.ToString(mydrv["起薪"]); 软件开发网
            if (Convert.ToDouble(score) < 34297.00)//大家这里根据具体情况设置可能ToInt32等等

            {
                GridView1.Rows[i].Cells[4].BackColor = System.Drawing.Color.Red;
            }
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在GridView中,要实现某一行换颜色的效果,可以通过自定义Adapter来实现。具体步骤如下: 1. 首先,创建一个继承自BaseAdapter的自定义Adapter,并重写getView方法。 2. 在getView方法中,通过position参数获取到每一行的位置,然后判断该行是否需要换颜色。 3. 如果需要换颜色,可以设置一个标志位来记录当前行是否为需要换颜色的行。 4. 在getView方法中,通过判断标志位来为需要换颜色的行设置不同的背景颜色。 以下是一个简单的示例代码: ```java public class CustomAdapter extends BaseAdapter { private Context mContext; private List<String> mItemList; private boolean mShouldChangeColor; public CustomAdapter(Context context, List<String> itemList) { mContext = context; mItemList = itemList; } @Override public int getCount() { return mItemList.size(); } @Override public Object getItem(int position) { return mItemList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view; if (convertView == null) { view = LayoutInflater.from(mContext).inflate(R.layout.item_gridview, parent, false); } else { view = convertView; } TextView textView = view.findViewById(R.id.text_view); textView.setText(mItemList.get(position)); if (position == 2) { // 假设第3行需要换颜色 mShouldChangeColor = true; } else { mShouldChangeColor = false; } if (mShouldChangeColor) { view.setBackgroundColor(Color.RED); // 设置背景颜色 } else { view.setBackgroundColor(Color.WHITE); } return view; } } ``` 这样,在GridView的每一行中,如果需要换颜色的行,背景颜色将会被设置为红色;否则,背景颜色将会是白色。 ### 回答2: 要使GridView中的某一行换颜色,可以通过编写自定义的适配器(Adapter)实现。 首先,创建一个继承自BaseAdapter的适配器类。在该类中,我们需要重写getView()方法,用于自定义每个Grid Item的显示内容。 在getView()方法中,我们可以根据需要设置每个Grid Item的背景颜色。为了实现某一行换颜色的效果,我们可以在getView()方法中判断当前Grid Item所在的行号,若为目标行,则设置其背景颜色为所需的颜色,否则设置默认的背景颜色。 以下是一个简单示例的适配器代码: ``` public class MyAdapter extends BaseAdapter { private Context mContext; private List<String> mData; private int mTargetRow; // 目标行号 public MyAdapter(Context context, List<String> data, int targetRow) { mContext = context; mData = data; mTargetRow = targetRow; } @Override public int getCount() { return mData.size(); } @Override public Object getItem(int position) { return mData.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view; if (convertView == null) { view = LayoutInflater.from(mContext).inflate(R.layout.grid_item, parent, false); } else { view = convertView; } TextView textView = view.findViewById(R.id.text_view); textView.setText(mData.get(position)); // 判断当前Grid Item所在的行号是否为目标行 if (position / numColumns == mTargetRow) { view.setBackgroundColor(mContext.getResources().getColor(R.color.target_color)); } else { view.setBackgroundColor(mContext.getResources().getColor(R.color.default_color)); } return view; } } ``` 在上述代码中,我们通过设置mTargetRow变量来指定目标行号,在getView()方法中通过计算当前Grid Item所在的行号来判断是否为目标行,并设置不同的背景颜色。在实例化适配器时,我们需要传入相应的参数,包括目标行号和数据源。 注意,colors.xml文件中应该定义target_color和default_color两个颜色供适配器使用。 最后,将适配器设置给GridView: ``` GridView gridView = findViewById(R.id.grid_view); gridView.setAdapter(new MyAdapter(this, data, targetRow)); ``` 这样就实现了在GridView中某一行换颜色的效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值