我添加了一个动态生成的GridView。我从DB获取行并将其加载到DataTable中。我想为此DataTable添加编辑和删除按钮并将其绑定到GridView,以便我可以编辑和删除GridView的内容。 GridView生成不止一次,并在循环中调用,因为需要生成数周的时间表。以下是绑定数据和GridView生成的源代码:
protected void BindData(string str)
{
string[] startdate = getdate(str);
int firstdate = Convert.ToInt32(startdate[1]);
int lastdate = firstdate + 7;
string enddate = startdate[0]+"/"+Convert.ToString(lastdate)+"/"+startdate[2];
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT DISTINCT CONVERT (varchar(25), Cale_WeekDate,106) AS 'Date',Cale_DayName AS 'Day',Cale_Task8 AS '08:00 AM',Cale_Task9 AS '09:00 AM',Cale_Task10 AS '10:00 AM',Cale_Task11 AS '11:00 AM',Cale_Task12 AS '12:00 PM',Cale_Task13 AS '1:00 PM' FROM Calendar WHERE Cale_WeekDate BETWEEN '"+str+"' AND '"+enddate+"'", conn);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
GridView gvTimeSheet = new GridView();
//gvTimeSheet.ID = "gvTimeSheet" + gridcount;
gvTimeSheet.AutoGenerateColumns = false;
dt = ds.Tables[0];
for (int i = 0; i < dt.Columns.Count; i++)
{
BoundField boundfield = new BoundField();
boundfield.DataField = dt.Columns[i].ColumnName.ToString();
boundfield.HeaderText = dt.Columns[i].ColumnName.ToString();
gvTimeSheet.Columns.Add(boundfield);
}
gvTimeSheet.DataSource = dt;
gvTimeSheet.DataBind();
gvTimeSheet.Width = 600;
gvTimeSheet.HeaderStyle.CssClass = "header";
gvTimeSheet.RowStyle.CssClass = "rowstyle";
Panel1.Controls.Add(gvTimeSheet);
HtmlGenericControl linebreak = new HtmlGenericControl("br");
Panel1.Controls.Add(linebreak);
}
答案 0 :(得分:0)
在asp.net中我们可以这样做:
<asp:GridView>
<Columns>
<asp:TemplateField HeaderText="Test">
<ItemTemplate>
<asp:Button ID="btnTest" CommandArgument='btnTest' runat="server" Text="Test et" OnClick="btnTest_Click" />
<asp:Button ID="btnVerify" CommandArgument='btnVerify' runat="server" Text="Verify" OnClick="btnVerify_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
在代码方面:
protected void btnTest_Click(object sender, EventArgs e)
{
}
protected void btnVerify_Click(object sender, EventArgs e)
{
}
您还可以在运行时将按钮添加为列。
答案 1 :(得分:0)
取最大列数,假设您有3个数据绑定列(i = 3),在位置(i + 1 = 4)添加模板列进行编辑,在下一个模板列进行删除(i + 1 = 5) )。