GridView用法

GridView用法:
1、分页
属性:AllowPaging(设置是否允许分页) PageSize (设置每页的数量条数)。
GridView的PageIndexChanging事件
GridView1.PageIndex = e.NewPageIndex; //e是系统自定的
GrdBindData(); //重新绑定数据

2、删除数据(RowDeleting事件),修改数据(RowUpdating事件)
string CustomerID = GridView1.DataKeys[e.RowIndex].value.tostring();
DeleteCustomers(CustomerID);
/*如果是修改数据的话
如果不加上IsPostBack,取得的值是数据库中的字段,而不是GridView表格中TextBox的值,但如果加上IsPostBack就一切正常.
string CustomerID = GridView1.DataKeys[e.RowIndex][0].ToString();
string CompanyName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
string ContactName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
UpdateCustomers(CustomerID, CompanyName, ContactName, Address);
GridView1.EditIndex = -1;
*/
GrdBindData(); //重新绑定数据

3、进入编辑状态(RowEditing事件)
this.GridView1.EditIndex = e.NewEditIndex;
GrdBindData(); //重新绑定数据

4、终止编辑状态(RowCancelingEdit事件)
this.GridView1.EditIndex = -1;
GrdBindData(); //重新绑定数据

5、鼠标移到GridView某一行时改变该行的背景色的方法,添加行号的方法(RowDataBound事件)
//首先判断是否是数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//当鼠标停留时更改背景色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
//当鼠标移开时还原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
当有编辑列时,避免出错,要加的RowState判断
//if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
//{
// ((LinkButton)e.Row.Cells[3].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('您确定要删除:\"" + e.Row.Cells[1].Text + "\"吗?')");
//}
}
//添加行号,这时最好把前台的第一列的表头该为“编号”,因为以前的第一列被“吃掉”了。
if (e.Row.RowIndex != -1)
{
int id = e.Row.RowIndex + 1;
e.Row.Cells[0].Text = id.ToString();
}

6、GridView实现用“...”代替超长字符串 方法:数据绑定后过滤每一行即可
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
string gIntro;
if (GridView1.PageIndex == 0)
{
DataTable vDt = SqlHelper.GetDataTable();
gIntro = Convert.ToString(vDt.Rows[i][2]);//所要处理的字段
GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2);
}
//截取字符的方法
public string SubStr(string sString, int nLeng)
{
if (sString.Length <= nLeng)
{
return sString;
}
string sNewStr = sString.Substring(0, nLeng);
sNewStr = sNewStr + "...";
return sNewStr;
}

7、每行添加Checkbox,添加模板列即可
<asp:TemplateField>
<HeaderTemplate>选择</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>

8、全选的checkbox的CheckedChanged事件
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (CheckBox2.Checked == true)
{
cbox.Checked = true;
}
else
{
cbox.Checked = false;
}
}
9、删除所选的Button的onclick事件
int vNum=0;
using (SqlConnection conn = new SqlConnection(SqlConnStr))
{
conn.Open();
for (int i = 0; i < GridView1.Rows.Count - 1; i++)
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (cbox.Checked == true)
{
DelSelect(Convert.ToInt32(GridView1.DataKeys[i].Value));
vNum++;
}
}
}
GridDataBind();
ClientScript.RegisterStartupScript(this.GetType(), "", string.Format("<script language='javascript' type='text/javascript'>alert('删除了{0}条记录');</script>", vNum));

10、在winform中gridview改了个名字叫DataGridview,绘制行号的方法如下:
private void gvdata_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{

Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
Convert.ToInt32(e.RowBounds.Location.Y + (e.RowBounds.Height - gvdata.RowHeadersDefaultCellStyle.Font.Size) / 2),
gvdata.RowHeadersWidth - 4, e.RowBounds.Height);
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
gvdata.RowHeadersDefaultCellStyle.Font, rectangle, gvdata.RowHeadersDefaultCellStyle.ForeColor,
TextFormatFlags.Right);
}

11、gridview空数据时 也显示列头
<asp:GridView ID="GridView1" runat="server" >
<EmptyDataTemplate>
<table>
<tr>

<th>Id</th>
<th>Name</th>
<th>Department</th>
</tr>
<tr>
<td colspan="3">
对不起,没有找到任何相关记录
</td>
</tr>
</table>
</EmptyDataTemplate>
<Columns>
<asp:TemplateField>
<HeaderTemplate>ID</HeaderTemplate>
<ItemTemplate><%#eval_r("Id") %></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>Name</HeaderTemplate>
<ItemTemplate><%#eval_r("Name") %></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate> Department</HeaderTemplate>
<ItemTemplate><%#eval_r("Department") %></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
GridView是Android中常用的布局控件之一,用于在屏幕上展示多行多列的数据,类似于网格的形式。下面是GridView的使用方法: 1. 在布局文件中添加GridView控件 ```xml <GridView android:id="@+id/grid_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:numColumns="3" android:verticalSpacing="10dp" android:horizontalSpacing="10dp" android:padding="10dp" android:columnWidth="100dp" /> ``` 其中`numColumns`属性表示每行显示的列数,`verticalSpacing`和`horizontalSpacing`属性表示网格之间的垂直和水平间距,`padding`属性表示GridView的内边距,`columnWidth`属性表示每个网格的宽度。 2. 创建适配器Adapter GridView需要一个适配器来提供数据,可以使用ArrayAdapter或BaseAdapter。这里以BaseAdapter为例: ```java public class MyAdapter extends BaseAdapter { private Context mContext; private List<String> mData; public MyAdapter(Context context, List<String> data) { this.mContext = context; this.mData = data; } @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) { ViewHolder holder; if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(R.layout.grid_item, null); holder = new ViewHolder(); holder.textView = convertView.findViewById(R.id.text_view); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.textView.setText(mData.get(position)); return convertView; } static class ViewHolder { TextView textView; } } ``` 适配器中重写了BaseAdapter的四个方法,其中`getView()`方法用于设置每个网格的内容。 3. 设置适配器Adapter 在Activity或Fragment中设置适配器: ```java GridView gridView = findViewById(R.id.grid_view); List<String> data = new ArrayList<>(); data.add("Item 1"); data.add("Item 2"); data.add("Item 3"); data.add("Item 4"); MyAdapter adapter = new MyAdapter(this, data); gridView.setAdapter(adapter); ``` 运行程序即可看到GridView中展示了四个网格,每个网格中显示了对应的文本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值