WebForm我全部动态产生一个datagrid,怎么加入按钮列,还有怎么取得它的事件?我昨天研究了一天,没什么突破,我查找了以前的帖子,没发现

心雨楼 2003-10-29 11:20:32
现在实现动态产生一个datagrid我写了个类可以加入模板列,但不知道怎么加入按钮列,跟静态的datagrid 的编辑按钮一样可以写更新和取消的事件,但是我使用代码写产生的datagrid,不知道怎么产生按钮列?还有相应事件等

总之能实现 "增 删 改"的功能就完事OK!

如果以上能够实现

那么我想产生不确定数目的datagrid,都邦定两个字段,怎么响应事件怎么办?,



...全文
45 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
acewang 2003-11-04
  • 打赏
  • 举报
回复
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_20693715.html
haoliqi 2003-11-04
  • 打赏
  • 举报
回复
例:产生按钮列
private void CreateButtonColumn()
{
ButtonColumn dgc=new ButtonColumn ();
dgc.ButtonType =ButtonColumnType.LinkButton ;
dgc.CommandName="Select";
dgc.HeaderStyle .BackColor =Color.RoyalBlue ;
dgc.Text ="选择";

dg.Columns .Add (dgc);
}
在为它加个事件
this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e){

}


心雨楼 2003-11-04
  • 打赏
  • 举报
回复
这个问题好像没什么好方法?

wolve 2003-10-30
  • 打赏
  • 举报
回复
有是有办法,不过我不推荐你这样做,这种方法又把我们带回asp中的老路上了。
我建议不要动态添加,不用时你隐藏就行了,动态添加的控件是不会引发回发事件的
ruanyuping 2003-10-30
  • 打赏
  • 举报
回复
http://chs.gotdotnet.com/quickstart/aspplus/
心雨楼 2003-10-30
  • 打赏
  • 举报
回复
大家似乎不太理解我说的意思!是这样的!
我动态产生 DataGrid mygrid = new DataGrid();
给Grid加列 mygrid.cloumns.add(mytemplatecol);//mytemplatecol是我写一个类产生的模板列;

问题是我不会产生一个按钮列,像静态DataGrid的按钮列一样有编辑更新取消按钮,还有就是这些按钮事件怎样获得!

那位有这方面的资料,指点一下阿!感激不尽...
gordenfl 2003-10-30
  • 打赏
  • 举报
回复
<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" Width="40px">
<Columns>
<asp:TemplateColumn>
<ItemStyle Wrap="False"></ItemStyle>
<ItemTemplate>
<asp:Label id="lbl1" runat="server"/>
<table>
<tr>
<td><asp:Literal id="lit1" runat=server /><td>
<td><asp:Literal id="lit2" runat=server /><td>
</tr>
<tr>
<td colspan="2"><asp:Literal id="lit3" runat=server /><td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>



in DataGrid's ItemDataBound event handler, do

void DataGrid1_OnItemDataBound(Object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
string s1= DataBinder.Eval(e.Item.DataItem, "ID").ToString();
Label lbl = (Label)e.FindControl("lbl1");
lbl1.Text = s1;

string s2= DataBinder.Eval(e.Item.DataItem, "user").ToString();
Literal lit1 = (Literal)e.FindControl("lit1");
lit1.Text = s2;

string s3= DataBinder.Eval(e.Item.DataItem, "Tax").ToString();
Literal lit2 = (Literal)e.FindControl("lit2");
lit2.Text = s3;

string s4= DataBinder.Eval(e.Item.DataItem, "addr").ToString();
Literal lit3 = (Literal)e.FindControl("lit3");
lit3.Text = s4;

}
}

kjphe 2003-10-29
  • 打赏
  • 举报
回复
<asp:TemplateColumn>
<ItemTemplate>
<asp:Button Runat=server text="name" CommandName='<% DataBinder.Eval(Container.DataItem,"表关键字")%>'></asp:Button>
</ItemTemplate>
</asp:TemplateColumn>
然后在后台做事件就可以
elite2018 2003-10-29
  • 打赏
  • 举报
回复
public DataGrid MakeGrid()
{
mygrid.CellPadding=2;
mygrid.Attributes.Add("align","center");
mygrid.CellSpacing=0;
mygrid.Width=500;
mygrid.BorderWidth=1;
mygrid.BorderColor=ColorTranslator.FromHtml("Black");
mygrid.AutoGenerateColumns=false;
mygrid.ForeColor=ColorTranslator.FromHtml("Black");
mygrid.Font.Size=9;
mygrid.Font.Name="宋体";
mygrid.AllowSorting=true;

///sort命令的事件处理器

mygrid.SortCommand += new DataGridSortCommandEventHandler(Sort_Grid);

///设置headerstyle
mygrid.HeaderStyle.BackColor=ColorTranslator.FromHtml("Gold");
mygrid.HeaderStyle.ForeColor=ColorTranslator.FromHtml("Black");
mygrid.HeaderStyle.Font.Name="宋体";
mygrid.HeaderStyle.Font.Size=9;
mygrid.HeaderStyle.Font.Bold=true;
mygrid.HeaderStyle.HorizontalAlign=HorizontalAlign.Center;
rgbcn 2003-10-29
  • 打赏
  • 举报
回复
see
http://www.tongyi.net/article/20020121/200201213034.shtml
rgbcn 2003-10-29
  • 打赏
  • 举报
回复
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>

<script language="C#" runat="server">

SqlConnection myConnection;

protected void Page_Load(Object Src, EventArgs E)
{
myConnection = new SqlConnection("server=(local)\\NetSDK;database=pubs;Trusted_Connection=yes");

if (!IsPostBack)
BindGrid();
}

public void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs e)
{
MyDataGrid.EditItemIndex = (int)e.Item.ItemIndex;

BindGrid();

for (int i = 0; i < e.Item.Cells.Count; i++)
{

e.Item.Cells[i].Text = Server.HtmlDecode(e.Item.Cells[i].Text);
Trace.Write(e.Item.Cells[i].Text);
}
}

public void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs e)
{
MyDataGrid.EditItemIndex = -1;
BindGrid();
}

public void MyDataGrid_Update(Object sender, DataGridCommandEventArgs e)
{
String updateCmd = "UPDATE Authors SET au_id = @Id, au_lname = @LName, au_fname = @FName, phone = @Phone, "
+ "address = @Address, city = @City, state = @State, zip = @Zip, contract = @Contract where au_id = @Id";

SqlCommand myCommand = new SqlCommand(updateCmd, myConnection);

myCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.NVarChar, 11));
myCommand.Parameters.Add(new SqlParameter("@LName", SqlDbType.NVarChar, 40));
myCommand.Parameters.Add(new SqlParameter("@FName", SqlDbType.NVarChar, 20));
myCommand.Parameters.Add(new SqlParameter("@Phone", SqlDbType.NChar, 12));
myCommand.Parameters.Add(new SqlParameter("@Address", SqlDbType.NVarChar, 40));
myCommand.Parameters.Add(new SqlParameter("@City", SqlDbType.NVarChar, 20));
myCommand.Parameters.Add(new SqlParameter("@State", SqlDbType.NChar, 2));
myCommand.Parameters.Add(new SqlParameter("@Zip", SqlDbType.NChar, 5));
myCommand.Parameters.Add(new SqlParameter("@Contract", SqlDbType.NVarChar,1));

myCommand.Parameters["@Id"].Value = MyDataGrid.DataKeys[(int)e.Item.ItemIndex];

String[] cols = {"@Id","@LName","@FName","@Phone","@Address","@City","@State","@Zip","@Contract"};

int numCols = e.Item.Cells.Count;
for (int i=2; i<numCols-1; i++) //skip first, second and last column
{
String colvalue =((TextBox)e.Item.Cells[i].Controls[0]).Text;

// check for null values in required fields
if (i<6 && colvalue == "")
{
Message.InnerHtml = "ERROR: Null values not allowed for Author ID, Name or Phone";
Message.Style["color"] = "red";
return;
}

myCommand.Parameters[cols[i-1]].Value = Server.HtmlEncode(colvalue);
}

//append last row, converting true/false values to 0/1
if (String.Compare(((TextBox)e.Item.Cells[numCols-1].Controls[0]).Text, "true", true)==0)
myCommand.Parameters["@Contract"].Value = "1";
else
myCommand.Parameters["@Contract"].Value = "0";

myCommand.Connection.Open();

try
{
myCommand.ExecuteNonQuery();
Message.InnerHtml = "<b>Record Updated</b><br>" + updateCmd;
MyDataGrid.EditItemIndex = -1;
}
catch (SqlException exc)
{
if (exc.Number == 2627)
Message.InnerHtml = "ERROR: A record already exists with the same primary key";
else
Message.InnerHtml = "ERROR: Could not update record, please ensure the fields are correctly filled out";
Message.Style["color"] = "red";
}

myCommand.Connection.Close();

BindGrid();
}

private void MyDataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{
for (int i = 0; i < e.Item.Controls.Count; i++)
{
if (e.Item.Controls[i].Controls[0].GetType().ToString() == "System.Web.UI.WebControls.TextBox")
{
TextBox tb = (TextBox)e.Item.Controls[i].Controls[0];
tb.Text = Server.HtmlDecode(tb.Text);
}
}
}
}

public void BindGrid()
{
SqlDataAdapter myCommand = new SqlDataAdapter("select * from Authors", myConnection);

DataSet ds = new DataSet();
myCommand.Fill(ds, "Authors");

MyDataGrid.DataSource=ds.Tables["Authors"].DefaultView;
MyDataGrid.DataBind();
}

</script>

<body style="font: 10pt verdana">

<form runat="server">

<h3><font face="Verdana">Updating a Row of Data</font></h3>

<span id="Message" EnableViewState="false" style="font: arial 11pt;" runat="server"/><p>

<ASP:DataGrid id="MyDataGrid" runat="server"
Width="800"
BackColor="#ccccff"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
OnEditCommand="MyDataGrid_Edit"
OnCancelCommand="MyDataGrid_Cancel"
OnUpdateCommand="MyDataGrid_Update"
DataKeyField="au_id"
OnItemDataBound="MyDataGrid_ItemDataBound"
>

<Columns>
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" ItemStyle-Wrap="false"/>
</Columns>

</ASP:DataGrid>

</form>

</body>
</html>

softye 2003-10-29
  • 打赏
  • 举报
回复
看看这个
http://expert.csdn.net/Expert/topic/2397/2397807.xml?temp=.9014398
心雨楼 2003-10-29
  • 打赏
  • 举报
回复
今天怎么人这么少呢?帮帮忙阿!提供点资料也行啊!感激不尽...
心雨楼 2003-10-29
  • 打赏
  • 举报
回复
我的模板动态产生,总之所有都是在后端代码写的!

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧