vs2003 DataGrid 使用技巧
来源:互联网 发布:windows xp ftp服务器 编辑:程序博客网 时间:2024/05/22 11:42
//删除DataGrid 表格选定记录----
int intEmpId=(int)myDataGrid.DataKeys[e.Item.ItemIndex];
string deleteCmd="Delete from Employee where emp_id="+intEmpId.Tostring()+""
//点击表格打开
DatagriddataDateBound()
if(e.item.ItemType==ListItemType.Item||e.item.itemType==ListItemType.AlternatingItem)
e.item.attributes.add("onclick","window.open('Default.aspx?id="+e.item.Cells[0].Text+"');");
//双击表格连接到另一页
//在ItemDataBind事件中
if(e.item.ItemType==ListItemType.Item||e.item.itemType==ListItemType.AlternatingItem)
{
string OrderItemId=e.item.cells[1].Text;
e.item.Attributes.add("ondbclick","location.href='...'");
}
//双击表格打开新一页
if(e.item.ItemType==ListItemType.Item||e.item.itemType==ListItemType.AlternatingItem)
{
string OrderItemId=e,item.cells[1].Text;
e.item.attibute.add("ondbclick","open()");
}
//表格超级连接列传递参数-----------------------
<asp:HyperLinkColumn Target="_blank" headtext="ID号" DataTextField="id"
NavigateUrl="aaa.aspx?
id='<%# DataBinder.Eval(Container.DataItem,"数据字段1")%>' &name='<%# DataBind.Eval(Container.DataItem,"数据字段2")%>' />
//添加全选模板列
protect void checkall_checkchanged(object sender,system.eventargs e)
{
//改变列的选定,事先全选或全不选
CheckBox chkExport;
if(checkall.checked)
{
foreach(DataGridItem oDataGridItem in MyDataGrid.items)
{
chkExport=(CheckBox)oDataGridItem.FindControl("chkExport");
chkExport.Checked=true;
}
else
{
foreach(DataGridItem oDataGridItem in MyDataGrid.items)
{
chkExport=(CheckBox)oDataGridItem.FindControl("chkExport");
chkExport.Checked=false;
}
}
}
}
//编辑文本框的大小-----------
private void DataGrid1_ItemDataBound(obj sender,DataGridItemEventArgs e)
{
for(int i=0;i<e.item.Cells.Count-1;i++)
{
if(e.item.ItemType==ListItemType.EditType)
{
e.Item.Cells[i].Attributes.add("width","80px")
}
}
}
自定义分页--------------------------------
public static int pageCount;//总页面数
public static int curPageIndex=1;//当前页面
//下一页
if(DataGrid1.CurrentPageIndex<(DataGrid1.PageCount=1))
{
DataGrid1.CurrentPageIndex+=1;
curPageIndex+=1;
}
bind();//DataGrid1数据绑定函数
//上一页
if(DataGrid1.CurrentPageIndex>0)
{
DataGrid1.CurrentPageIndex+=1;
curPageIndex-=1;
}
bind();//DataGrid1数据绑定函数
//直接页面跳转
int a=int.Parse(JumpPage.Value.Trim());//JumpPage.Value.Trim()为跳转值
if(a<DataGrid1.PageCount)
{
this.DataGrid1.CurrentPageIndex=a;
}
bind();//DataGrid1数据绑定函数
//DataGrid 的删除提示
e.Item.Cells(1).Attributes("onclick") = "javascript:if (confirm('确定删除" + Trim(e.Item.Cells(2).Text) + "(" + Trim(e.Item.Cells(7).Text) + ")?')) {return true;} else {return false;};"
If e.Item.ItemType <> UI.WebControls.ListItemType.Header Then
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=""" + e.Item.Style("BACKGROUND-COLOR") + """")
e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor=""" + "#EFF3F7" + """")
End If
If e.Item.ItemType = UI.WebControls.ListItemType.Item Or e.Item.ItemType = UI.WebControls.ListItemType.AlternatingItem Then
e.Item.Attributes.Add("onmouseover", "tdOver(this)")
e.Item.Attributes.Add("onmouseout", "tdOut(this)")
e.Item.Attributes.Add("onclick", "tdColor(this)")
e.Item.Attributes.Add("ondblclick", "tdColorDbl(this)")
End If
---Edit
private void dgShow_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgShow.EditItemIndex = e.Item.ItemIndex;
BindData();
}
---cancel
private void dgShow_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgShow.EditItemIndex = -1;
BindData();
}
--delete
private void dgShow_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(dgShow.Items.Count==1)
{
if(dgShow.CurrentPageIndex!=0)
dgShow.CurrentPageIndex = dgShow.CurrentPageIndex-1;
}
string strSql = "delete from tbStudentinfo where studentid="+e.Item.Cells[0].Text+"";
ExecuteSql(strSql);
BindData();
}
---update
private void dgShow_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string strStudentID = e.Item.Cells[0].Text;//處于非編輯狀態
string strName = ((TextBox)(e.Item.Cells[1].Controls[0])).Text;//處于編輯狀態
string strPass =((TextBox)(e.Item.Cells[2].Controls[0])).Text;
string strSex = ((CheckBox)(e.Item.Cells[3].FindControl("cbSex"))).Checked?"1":"0";
string strBirthday =((TextBox)(e.Item.Cells[4].Controls[0])).Text;
string strEmail =((TextBox)(e.Item.Cells[5].Controls[0])).Text;
strSql +=",Sex="+strSex+",Birthday='"+strBirthday+"',Email='"+strEmail+"' where studentid="+strStudentID+"";
dgShow.EditItemIndex = -1;
BindData();
}
--設置編輯框的格式
private void dgShow_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{
for (int i=0;i<e.Item.Cells.Count;i++)
{
if(e.Item.Cells[i].Controls.Count>0)
{
try
{
TextBox t =(TextBox)e.Item.Cells[i].Controls[0];
t.Width=50;
}catch(Exception ee)
{
}
}
}
}
}
<ItemTemplate>
<asp:RadioButton id=RadioButton2 runat="server" Enabled="False"
Checked='<%# DataBinder.Eval(Container, "DataItem.Sex") %>'Text="男">
</asp:RadioButton>
<asp:RadioButton id=RadioButton1 runat="server" Enabled="False"
Checked='<%# !(bool)DataBinder.Eval(Container, "DataItem.Sex") %>' Text="女">
</asp:RadioButton>
</ItemTemplate>
-----DataGrid 超連接列 ---連接到show.aspx頁面
<asp:HyperLinkColumn Text="點擊查看"
DataNavigateUrlField="StudentID"
DataNavigateUrlFormatString="Show.aspx?ID={0}"
DataTextField="StudentName" HeaderText="詳細信息">
</asp:HyperLinkColumn>
private void btnMIME_Click(object sender, System.EventArgs e)
{
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
int nCur = dgShow.CurrentPageIndex;
int nSize = dgShow.PageSize;
dgShow.AllowPaging = false;
BindData();
dgShow.Columns[7].Visible =false;
dgShow.RenderControl(hw);
dgShow.Columns[7].Visible =true;
//以下恢復分頁
dgShow.AllowPaging = true;
dgShow.CurrentPageIndex = nCur;
dgShow.PageSize = nSize;
BindData();
Response.Write(sw.ToString());
Response.End();
}
<asp:BoundColumn DataField="Birthday" HeaderText="生日" DataFormatString="{0:yyyy-M-d}"></asp:BoundColumn>
----DataGrid 實現 選擇刪除
選擇
public void CheckAll(object sender, System.EventArgs e)
{
CheckBox cbAll = (CheckBox)sender;
if(cbAll.Text=="全選")
{
foreach(DataGridItem dgi in dgShow.Items)
{
CheckBox cb = (CheckBox)dgi.FindControl("cbSelect");
cb.Checked = cbAll.Checked;
}
}
}
刪除
private void btnDelete_Click(object sender, System.EventArgs e)
{
foreach(DataGridItem dgi in dgShow.Items)
{
CheckBox cb = (CheckBox)dgi.FindControl("cbSelect");
if(cb.Checked)
{
//以下執行刪除操作
int nID = int.Parse(dgi.Cells[0].Text);
string strSql = "delete from tbStudentinfo where studentid="+nID;
ExecuteSql(strSql);
}
}
dgShow.CurrentPageIndex = 0;
BindData();
}
-----DataGrid 綁定自定義的下拉框進行編輯
1:<ItemTemplate>
<asp:DropDownList id="ddlSexI" runat="server" Enabled="False">
<asp:ListItem Value="1">男</asp:ListItem>
<asp:ListItem Value="0">女</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="ddlSexE" runat="server">
<asp:ListItem Value="1">男</asp:ListItem>
<asp:ListItem Value="0">女</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
foreach(DataGridItem dgi in dgShow.Items)
{
//以下綁定非編輯狀態下拉列表
DropDownList ddI = (DropDownList)dgi.FindControl("ddlSexI");
if(ddI!=null)
{
bool bSex = (bool)ds.Tables["studentinfo"].Rows[dgi.ItemIndex]["Sex"];
if(bSex)
ddI.SelectedIndex = 0;
else
ddI.SelectedIndex = 1;
}
//以下綁定編輯狀態下拉列表
DropDownList ddE = (DropDownList)dgi.FindControl("ddlSexE");
if(ddE!=null)
{
bool bSex = (bool)ds.Tables["studentinfo"].Rows[dgi.ItemIndex]["Sex"];
if(bSex)
ddE.SelectedIndex = 0;
else
ddE.SelectedIndex = 1;
}
3:
更新
-----DataGrid 實現自定義刪除鍵
創建
private void dgShow_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
switch(e.Item.ItemType)
{
case ListItemType.Item:
case ListItemType.EditItem:
case ListItemType.AlternatingItem:
Button myDeleteButton = (Button)e.Item.FindControl("btnDelete");
myDeleteButton.Text = "刪除此行";
myDeleteButton.Attributes.Add("onclick", "return confirm('您真的要刪除第 " + e.Item.ItemIndex.ToString() + " 行嗎?');");
break;
}
}
刪除
private void dgShow_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.CommandName=="UserDelete")
dgShow_DeleteCommand(source,e);
}
---style.backgroundColor ---設置DataGrid一行的顏色
If e.Item.ItemType <> UI.WebControls.ListItemType.Header Then
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=""" + e.Item.Style("BACKGROUND-COLOR") + """")
e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor=""" + "#EFF3F7" + """")
End If
- vs2003 DataGrid 使用技巧
- DataGrid使用技巧(一)
- DataGrid使用技巧大杂烩
- DataGrid使用技巧大杂烩
- DataGrid使用技巧
- DataGrid使用技巧
- DataGrid使用技巧
- DataGrid使用技巧
- DataGrid使用技巧大杂烩
- DataGrid使用技巧(一)
- DataGrid使用技巧大杂烩
- DataGrid使用技巧大杂烩
- DataGrid使用技巧大杂烩
- DataGrid使用技巧大杂烩
- DataGrid使用技巧大杂烩
- DataGrid 使用技巧
- DataGrid控件使用技巧
- DataGrid使用技巧(二)
- Android Activity设置全屏
- C#实现数据加密
- 一个大学女生发人深省的人生感悟
- Oracle 数据库创建表空间、创建用户指定表空间
- 电脑运行命令全集
- vs2003 DataGrid 使用技巧
- 中日少年十五年的较量 我们的教育总是把目标定在天上
- 大规模定制(Mass Customization,MC)
- 不可能测试的用户测试开始了
- 好好爱自己
- java程序员面试必备的32个要点 链接
- 赋值函数
- CProgressCtrl Class
- jQuery是什么