ASP.NET实现增删改查等功能(Access版)系统之二-GridView绑定数据.
2010-05-08 17:13
1091 查看
GridView绑定数据.
//绑定
public void bind(string str)
{
string oledbstr = "";
if (str != null && str != "")
{
oledbstr = str;
}
else
{
oledbstr = "select ID, 姓名,性别,出生日期,工作年限,证件类型,证件号,居住地,Email,手机号码,家庭电话,自我评价 from MResume order by id desc";
}
OleDbConnection oledbcon = new OleDbConnection(GetConnection());
OleDbDataAdapter myda = new OleDbDataAdapter(oledbstr, oledbcon);
DataSet myds = new DataSet();
oledbcon.Open();
myda.Fill(myds, "MResume");
GridView1.DataSource = myds.Tables[0]; //重点要记住的地方,看这个地方是DataSet or DataTable
GridView1.DataKeyNames = new string[] {"id"};//主键
GridView1.DataBind();
oledbcon.Close();
}
GridView行绑定
/// <summary>
/// 改变背景色,第二个方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowDataBound2(object sender, GridViewRowEventArgs e)
{
int i;
//执行循环,保证每条数据都可以更新
for (i = 0; i < GridView1.Rows.Count; i++)
{
//首先判断是否是数据行
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");
}
}
//如果是绑定数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//鼠标经过时,行背景色变 00A9FF E6F5FA
// e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#00A9FF'");
//鼠标移出时,行背景色变
// e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
}
//如果是绑定数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
//删除提示操作
((LinkButton)e.Row.Cells[13].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:\"" + e.Row.Cells[1].Text + "\"吗?')");
e.Row.Cells[11].Text = SubStr(e.Row.Cells[11].Text, 20); //用...代替长文本
}
}
//GridView实现自动编号:
if (e.Row.RowIndex != -1)
{
int id = e.Row.RowIndex + 1;
e.Row.Cells[0].Text = id.ToString();
}
//设置编辑状态文本框长度
//if ((e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)) || (e.Row.RowState == DataControlRowState.Edit))
//{
// TextBox curText;
// for (int j = 4; j < 12; j++)
// {
// curText = (TextBox)e.Row.Cells[j].Controls[0];
// curText.Width = Unit.Pixel(60); //设置长度
// curText.Font.Size = FontUnit.Point(11);//设置字号
// }
//}
}
public string SubStr(string sString, int nLeng)
{
if (sString.Length <= nLeng)
{
return sString;
}
string sNewStr = sString.Substring(0, nLeng);
sNewStr = sNewStr + "...";
return sNewStr;
}
//绑定
public void bind(string str)
{
string oledbstr = "";
if (str != null && str != "")
{
oledbstr = str;
}
else
{
oledbstr = "select ID, 姓名,性别,出生日期,工作年限,证件类型,证件号,居住地,Email,手机号码,家庭电话,自我评价 from MResume order by id desc";
}
OleDbConnection oledbcon = new OleDbConnection(GetConnection());
OleDbDataAdapter myda = new OleDbDataAdapter(oledbstr, oledbcon);
DataSet myds = new DataSet();
oledbcon.Open();
myda.Fill(myds, "MResume");
GridView1.DataSource = myds.Tables[0]; //重点要记住的地方,看这个地方是DataSet or DataTable
GridView1.DataKeyNames = new string[] {"id"};//主键
GridView1.DataBind();
oledbcon.Close();
}
GridView行绑定
/// <summary>
/// 改变背景色,第二个方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowDataBound2(object sender, GridViewRowEventArgs e)
{
int i;
//执行循环,保证每条数据都可以更新
for (i = 0; i < GridView1.Rows.Count; i++)
{
//首先判断是否是数据行
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");
}
}
//如果是绑定数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//鼠标经过时,行背景色变 00A9FF E6F5FA
// e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#00A9FF'");
//鼠标移出时,行背景色变
// e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
}
//如果是绑定数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
//删除提示操作
((LinkButton)e.Row.Cells[13].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:\"" + e.Row.Cells[1].Text + "\"吗?')");
e.Row.Cells[11].Text = SubStr(e.Row.Cells[11].Text, 20); //用...代替长文本
}
}
//GridView实现自动编号:
if (e.Row.RowIndex != -1)
{
int id = e.Row.RowIndex + 1;
e.Row.Cells[0].Text = id.ToString();
}
//设置编辑状态文本框长度
//if ((e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)) || (e.Row.RowState == DataControlRowState.Edit))
//{
// TextBox curText;
// for (int j = 4; j < 12; j++)
// {
// curText = (TextBox)e.Row.Cells[j].Controls[0];
// curText.Width = Unit.Pixel(60); //设置长度
// curText.Font.Size = FontUnit.Point(11);//设置字号
// }
//}
}
public string SubStr(string sString, int nLeng)
{
if (sString.Length <= nLeng)
{
return sString;
}
string sNewStr = sString.Substring(0, nLeng);
sNewStr = sNewStr + "...";
return sNewStr;
}
相关文章推荐
- ASP.NET实现增删改查等功能(Access版)系统之二-gridview绑定数据及行绑定
- ASP.NET实现增删改查等功能(Access版)系统之六-导出数据到EXCEL
- ASP.NET实现增删改查等功能(Access版)系统之六-导出数据到EXCEL
- ASP.NET实现增删改查等功能(Access版)系统之三-分页及排序
- ASP.NET实现增删改查等功能(Access版)系统之四-删除
- ASP.NET实现增删改查等功能(Access版)系统之一 -前台及数据库webconfig配置
- ASP.NET实现增删改查等功能(Access版)系统之五-更新
- ASP.NET实现增删改查等功能(Access版)系统之一
- ASP.NET实现增删改查等功能(Access版)系统之三-分页及排序
- ASP.NET实现增删改查等功能(Access版)系统之四-删除
- ASP.NET实现增删改查等功能(Access版)系统之五-更新
- ASP.NET + EXTJS 实现类似GridView行内编辑、Pannel、分页、增删改查、数据验证,格式转换
- ASP.NET中GridView动态绑定数据实现编辑更新
- 通过AJAX与ASP.NET结合实现的仿GridView增删改查功能!
- ASP.NET实现增删改查等功能(Access版)
- 通过AJAX与ASP.NET结合实现的仿GridView增删改查功能
- Asp.net使用jQuery实现Gridview, Repeater异步绑定数据
- GridView 和 Access数据库实现数据绑定(asp.net)
- 通过AJAX与ASP.NET结合实现的仿GridView增删改查功能! 推荐
- asp.net 站内搜索功能的实现及利用datatable绑定数据并进行分页