DataGrid小技巧

发表于:2007-06-30来源:作者:点击数: 标签:
//添加删除确认对话框: private void DataGrid1_ItemDataBound( ) { switch(e.Item.ItemType) { case ListItemType.Item: case ListItemType.AlternatingItem: case ListItemType.EditItem: ImageButton btn = (ImageButton)e.Item.FindControl(btnDelete);
//添加删除确认对话框:
private void DataGrid1_ItemDataBound( )

{
switch(e.Item.ItemType)
{
case ListItemType.Item:
case ListItemType.AlternatingItem:
case ListItemType.EditItem:
ImageButton btn = (ImageButton)e.Item.FindControl("btnDelete");
btn.Attributes.Add("onclick", "return confirm(@#你是否确定删除这条记录?@#);");
break;

}
}
处理操作:
private void DataGrid1_ItemCommand()
{
string c=e.CommandName;
int PermissionsID =(int)this.DataGrid1.DataKeys[e.Item.ItemIndex];//DataKeyField值
switch(c)
{
case "Delete":
//执行删除操作
break;

}

}

还有个好方法:
datagrid-》属性生成器-》列-》添加按钮列-》删除-》文本(T)->在文本框里加上:

<div id="de" onclick="JavaScript:return confirm(@#确定删除吗?@#)">删除</div>

就可以了/
OK!


//重新取值赋值:

private void DataGrid1_ItemDataBound( )
{
if((e.Item.ItemType==ListItemType.Item)||(e.Item.ItemType==ListItemType.AlternatingItem))
{
//e.Item代表一行

string ImgUrl = (string)DataBinder.Eval(e.Item.DataItem, "ImageUrl");
//注意这一行,"ImageUrl"字段是什么类型前面就转成什么类型,否则报错

//e.Item.Cells[0].Style.Add("font-weight", "bold");
//e.Item.Cells[0].ForeColor = System.Drawing.Color.Red;
//e.Item.BackColor = System.Drawing.Color.AliceBlue;


string s=e.Item.DataItem.ToString();
string ss=e.Item.Cells[0].Text;
int sss=e.Item.Cells.Count;

}
}


//绑定是重新计算页数与当前页

public void dataShowBind(DataSet ds)
{
this.DataGrid1.DataSource=ds;
if(ds.Tables.Count>0)
{
int rowscount=ds.Tables[0].Rows.Count;
double p=double.Parse(rowscount.ToString())/5.0;
//注意:只有double/double结果才是double,int/int=int;

int newPageCount=int.Parse(Math.Ceiling(p).ToString());

if(this.DataGrid1.CurrentPageIndex>newPageCount-1)
{
this.DataGrid1.CurrentPageIndex--;
}

this.DataGrid1.DataBind();
}


}

列中的控件的事件首先反升到DataGrid1_ItemCommand事件,但要判断CommandName
private void TopicsGrid_ItemCommand(e)
{
string btn=e.CommandName;
int i=0;
switch(btn)
{
case "edit":
i=(int)this.TopicsGrid.DataKeys[e.Item.ItemIndex];
break;
case "delete":
i=(int)this.TopicsGrid.DataKeys[e.Item.ItemIndex];

break;
}
}


点击链接列弹出窗口

<Columns>
<asp:TemplateColumn>
<ItemTemplate>

<a href="javascript:var aa=window.open(@#<%# "Form2.aspx?id=" + DataBinder.Eval(Container.DataItem,"持股名称")%>@#,@#@#,@#width=310,height=280,top=160,left=350@#);aa.focus();">
<%#DataBinder.Eval(Container.DataItem,"持股名称")%>
</a>

</ItemTemplate>
</asp:TemplateColumn>
</Columns>

原文转自:http://www.ltesting.net