public class Grid:WebControl,INamingContainer
{
SqlCmd datacommand=new SqlCmd();
public DataGrid TheGrid=new DataGrid();
public Button delbutton=new Button();
public Button checkall=new Button();
// public Label PageLabel = new Label();
public int _pagesize;
public String _cells;
public String _headtxt;
public bool _edit=true;
public bool _del=true;
public String sql
{
get { return ViewState["datagridsql"].ToString();}
set { ViewState["datagridsql"] = value;}
}
public int pagesize
{
get { return _pagesize;}
set { _pagesize = value;}
}
public String cells
{
get { return _cells;}
set { _cells = value;}
}
public String headtext
{
get { return _headtxt;}
set { _headtxt = value;}
}
public bool edit
{
get { return _edit;}
set { _edit = value;}
}
public bool del
{
get { return _del;}
set { _del = value;}
}
public string[] thecel()
{
return cells.Split(',');
}
public string[] Head()
{
return headtext.Split(',');
}
public string[] columes()
{
string str="";
foreach(DataColumn myCol in dset().Tables[0].Columns)
{
str+=myCol.ColumnName+",";
}
str=str+",";
str=str.Replace(",,","");
return str.Split(',');
}
public String datadb()
{
Regex regexmail = new Regex(@"from (/w+)");
return regexmail.Match(sql).Groups[1].ToString();
}
public DataSet dset()
{
return datacommand.GetDataList(sql);
}
public void pagebind()
{
TheGrid.DataSource=dset();
TheGrid.DataBind();
}
private void selectall(object sender, EventArgs e)
{
for (int i=0;i<this.TheGrid.Items.Count;i++)
{
CheckBox CB=(CheckBox)this.TheGrid.Items[i].FindControl("del");
if (CB.Checked==false)
CB.Checked=true;
else
CB.Checked=false;
}
}
private void SubmitBtn_Click(object sender, EventArgs e)
{
for (int j=0;j<this.TheGrid.Items.Count;j++)
{
CheckBox CB=(CheckBox)this.TheGrid.Items[j].FindControl("del");
string lb=TheGrid.Items[j].Cells[0].Text;
if (CB.Checked==true)
datacommand.update("delete from "+datadb()+" where id="+int.Parse(lb)+"");
}
pagebind();
}
public void MyDataGrid_Page(Object sender, DataGridPageChangedEventArgs e)
{
TheGrid.CurrentPageIndex = e.NewPageIndex;
pagebind();
}
public void MyDataGrid_Update(Object sender, DataGridCommandEventArgs e)
{
String id;
id = e.Item.Cells[0].Text;
String sql1="select * from "+datadb()+" where id="+id+"";
DataSet ds=datacommand.GetDataList(sql1);
DataRow dr = ds.Tables[0].Rows[0];
//HttpContext.Current.Response.Write(id);
for (int i=0;i<thecel().Length;i++)
{
dr[thecel()[i]]=((TextBox)e.Item.Cells[i+2].Controls[0]).Text;
}
datacommand.updatetb(ds,sql1);
TheGrid.EditItemIndex = -1;
pagebind();
}
public void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs e)
{
TheGrid.EditItemIndex = (int)e.Item.ItemIndex;
e.Item.Attributes["style"]="background-color: #666666;";
pagebind();
}
public void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs e)
{
TheGrid.EditItemIndex = -1;
pagebind();
}
public Grid()
{
String style="margin-top:2px;margin-right:2px;border: 1px solid #AAB3B3;background-color: #FFFFFF;";
TheGrid.CellPadding=4;
TheGrid.CellSpacing=0;
TheGrid.AllowPaging=true;
TheGrid.AutoGenerateColumns=false;
TheGrid.PagerStyle.Mode=PagerMode.NumericPages;
TheGrid.HeaderStyle.BackColor=System.Drawing.Color.FromName("#A7B3B8");
TheGrid.PageIndexChanged+=new DataGridPageChangedEventHandler(this.MyDataGrid_Page);
TheGrid.EditCommand+=new DataGridCommandEventHandler(this.MyDataGrid_Edit);
TheGrid.CancelCommand+=new DataGridCommandEventHandler(this.MyDataGrid_Cancel);
TheGrid.UpdateCommand+=new DataGridCommandEventHandler(this.MyDataGrid_Update);
delbutton.Text="Del";
checkall.Text="Select All";
TheGrid.Attributes["style"]="width:100%";
delbutton.Attributes["style"]=style;
checkall.Attributes["style"]=style;
checkall.Click += new EventHandler(this.selectall);
delbutton.Click += new EventHandler(this.SubmitBtn_Click);
this.Controls.Add(this.TheGrid);
}
protected override void CreateChildControls()
{
TheGrid.PageSize=20;
if (pagesize>0)
TheGrid.PageSize=pagesize;
EditCommandColumn theedit=new EditCommandColumn();
theedit.EditText="编辑";
theedit.CancelText="取消";
theedit.UpdateText="更新";
theedit.ItemStyle.Wrap=false;
theedit.HeaderText="操作";
theedit.HeaderStyle.Wrap=false;
BoundColumn theid=new BoundColumn();
theid.HeaderText="ID";
theid.DataField="id";
theid.ReadOnly=true;
TheGrid.Columns.Add(theid);
TemplateColumn tm2=new TemplateColumn();
tm2.ItemTemplate=new checkbox();
tm2.HeaderText="选择";
if (del==false)
edit=false;
else
TheGrid.Columns.Add(tm2);
string[] col;
if (cells!=null && cells!="")
col=thecel();
else
col=columes();
for (int i=0;i<col.Length;i++)
{
//*
TemplateColumn tm=new TemplateColumn();
content t2=new content();
t2.field=col[i];
tm.ItemTemplate=t2;
TextBoxTemplate txtbox =new TextBoxTemplate();
txtbox.field=col[i];
tm.EditItemTemplate =txtbox;
if (headtext!=null && headtext!="")
tm.HeaderText=Head()[i];
else
tm.HeaderText=col[i];
if (col[i]!="id")
TheGrid.Columns.Add(tm);
//*/
}
if (edit!=false)
{
TheGrid.Columns.Add(theedit);
}
pagebind();
this.Controls.Add(TheGrid);
if (del==true)
{
this.Controls.Add(checkall);
this.Controls.Add(delbutton);
}
}
}//end of class
public class checkbox : ITemplate//选择列类
{
public void InstantiateIn(Control container)
{
CheckBox thecheck=new CheckBox();
thecheck.ID="del";
container.Controls.Add(thecheck);
}
}//end of class
public class content : ITemplate //显示列类
{
Root theroot=new Root();
public String _field;
public String field
{
get { return _field;}
set { _field = value;}
}
public void InstantiateIn(Control container)
{
Label l = new Label();
l.DataBinding += new EventHandler(this.OnDataBinding);
container.Controls.Add(l);
}
public void OnDataBinding(object sender, EventArgs e)
{
Label l = (Label) sender;//发送绑定请求
DataGridItem container = (DataGridItem) l.NamingContainer;
l.Text = theroot.getstring(((DataRowView)container.DataItem)[field].ToString(),50);//绑定字段
l.ToolTip =((DataRowView)container.DataItem)[field].ToString();
}
}//end of class
public class TextBoxTemplate : ITemplate //编辑列类
{
public String _field;
public String field
{
get { return _field;}
set { _field = value;}
}
public void InstantiateIn(Control container)
{
TextBox l = new TextBox();
l.Attributes["style"]="border: 1px solid #AAB3B3;background-color: #FFFFFF;";
l.DataBinding += new EventHandler(this.OnDataBinding);
container.Controls.Add(l);
}
public void OnDataBinding(object sender, EventArgs e)
{
TextBox l = (TextBox) sender;//发送绑定请求
DataGridItem container = (DataGridItem) l.NamingContainer;
l.Text = ((DataRowView)container.DataItem)[field].ToString();//绑定字段
}
}
}
生成控件确实是件麻烦事,好多方法不能用
看这里
/*
TemplateColumn tm=new TemplateColumn();
content t2=new content();
t2.field=thecel()[i];
tm.ItemTemplate=t2;
tm.HeaderText=thecel()[i];
if (thecel()[i]!="id")
TheGrid.Columns.Add(tm);
/*/
//*
BoundColumn tm=new BoundColumn();
tm.HeaderText=thecel()[i];
tm.DataField=thecel()[i];
if (thecel()[i]!="id")
TheGrid.Columns.Add(tm);
//*/
为什么换成模版后,编辑状态下就没用,不能编辑,奇怪
!-_-默认就是不能编辑的,需要编辑要单独设定
tm.EditItemTemplate =txtbox;