删除

<div onclick='return confirm("你确认删除吗?");'>删除</div>
private
void
Dg_DeleteCommand(
object
source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int
id
=
Convert.ToInt32(e.Item.Cells[
0
].Text);
QsqPageContent pc
=
new
QsqPageContent();
pc.Id
=
id;
DataAccessReturn pcD
=
pc.Delete();
Bind();
}
编辑
如果不允许编辑则勾选“只读”,如不想显示则勾选“可见”,计算item[]时要计算在内,仍然保留了其列号。
private
void
Dg_EditCommand(
object
source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
Dg.EditItemIndex
=
e.Item.ItemIndex;
Bind();
}
private
void
Dg_CancelCommand(
object
source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
Dg.EditItemIndex
=
-
1
;
Bind();
}
private
void
Dg_UpdateCommand(
object
source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int
ID
=
int
.Parse(e.Item.Cells[
0
].Text);
string
showName
=
((TextBox)e.Item.Cells[
3
].Controls[
0
]).Text;
int
orderBy
=
Components.Tools.TypeParse.StrToInt(((TextBox)e.Item.Cells[
4
].Controls[
0
]).Text,
0
);
QsqPageContent pc
=
new
QsqPageContent();
pc.Id
=
ID;
pc.ShowName
=
showName;
pc.OrderBy
=
orderBy;
pc.Update();
Dg.EditItemIndex
=
-
1
;
Bind();
}
改变datagrid列宽度
模板列:编辑时的宽度调整方式(Width="100px")绑定显示宽度同位置调整一样在第一行(
HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center")
Code
<asp:TemplateColumn HeaderText="状态" HeaderStyle-HorizontalAlign="Center" ItemStyle-
HorizontalAlign="Center">
<ItemTemplate>
<%#State(DataBinder.Eval(Container, "DataItem.State"))%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="DDLState" runat="server" AutoPostBack="True" Width="100px"><asp:ListItem Value="0">普通</asp:ListItem>
<asp:ListItem Value="1">New</asp:ListItem>
<asp:ListItem Value="2">Hot</asp:ListItem>
<asp:ListItem Value="12">New and Hot</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
绑定列:绑定显示宽度所有列都一样在html里调整,但编辑状态下的列宽度则在Dg_ItemDataBound
Code
<asp:BoundColumn DataField="OrderBy" HeaderText="排序" ItemStyle-Width="30px"></asp:BoundColumn>
编辑状态下
Code
private void Dg_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.EditItem)
{
TextBox txt = (TextBox)e.Item.Cells[3].Controls[0];
txt.Width = Unit.Pixel(150);
TextBox txt1 = (TextBox)e.Item.Cells[4].Controls[0];
txt1.Width = Unit.Pixel(20);
TextBox txt2 = (TextBox)e.Item.Cells[5].Controls[0];
txt2.Width = Unit.Pixel(180);
DropDownList ddl = e.Item.Cells[6].FindControl("DDLState") as DropDownList;//找到绑定列的ddl控件方式
if(ddl != null)
{
//隐藏列,保存ddl里的值,如果设为只读还可以写成直接获取它text,这样写麻烦些
string state = ((TextBox)(e.Item.Cells[7].Controls[0])).Text;
ListItem li = ddl.Items.FindByValue(state);
if(li != null)
{
li.Selected = true;
}
}
}
}
由于编辑状态下text都变成了textbox需要写成上面那样,如改为只读则 string state = e.Item.Cells[7].Text;
自动换行问题,正常字符的换行是比较合理的,而连续的数字和英文字符常常将容器撑大,挺让人头疼,
1.(IE浏览器)连续的英文字符和阿拉伯数字,使用word-wrap : break-word ;或者word-break:break-all;实现强制断行
Dg_ItemDataBound里
Code
if(e.Item.ItemType == ListItemType.AlternatingItem ||e.Item.ItemType==ListItemType.Item )
{
e.Item.Cells[5].Attributes.Add("style","word-break:break-all;");
}