在我的项目中,它是一个表格,显示了某人的某些细节以及执行某些操作的按钮。
表格如下:
-----------------------------
name | detail | other | Btn |
name | detail | other | Btn |
name | detail | other | Btn |
name | detail | other | Btn |
name | detail | other | Btn |
name | detail | other | Btn |
但是,我无法在Btn列前添加那些列,其中Btn列是使用asp GridView控件定义的,其他列是使用C#中的dataTable定义的。
因此,如何在C#中添加这些列?
代码: GridView的:
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" /> // if i remove these => errors
<asp:BoundField DataField="detail" HeaderText="detail" />
<asp:BoundField DataField="other" HeaderText="other" />
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgbtn" runat="server" ImageUrl="../Image/btn.png" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
代码:dataTable
var DT = new DataTable();
DT .Columns.Add("Name", typeof(string));// if i remove these => errors
DT .Columns.Add("Detail", typeof(string));
DT .Columns.Add("other", typeof(string));
.....foreach loop
DataRow dr = DT .NewRow();
dr["Name"] = item["Name"];
dr["Detail"] = item["Detail"];
dr["other"] = item["other"];
waitDT.Rows.Add(dr);
.....end of foreach loop
GridView1.DataSource = DT;
GridView1.DataBind();
所以,我得到的结果是:
-----------------------------------------------------
name | detail | other | Btn | name | detail | other |
name | detail | other | Btn | name | detail | other |
name | detail | other | Btn | name | detail | other |
name | detail | other | Btn | name | detail | other |
name | detail | other | Btn | name | detail | other |
name | detail | other | Btn | name | detail | other |
主要问题:
我想在asp控件中定义btn列,在C#中定义其他列。
它应该看起来像: <asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgbtn" runat="server" ImageUrl="../Image/btn.png" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
然后是C#:
var DT = new DataTable();
DT .Columns.Add("Name", typeof(string));//it should place in front of the btn column
DT .Columns.Add("Detail", typeof(string));
DT .Columns.Add("other", typeof(string));
.....foreach loop
DataRow dr = DT .NewRow();
dr["Name"] = item["Name"];
dr["Detail"] = item["Detail"];
dr["other"] = item["other"];
waitDT.Rows.Add(dr);
.....end of foreach loop
GridView1.DataSource = DT;
GridView1.DataBind();
最后结果:
-----------------------------
name | detail | other | Btn |
name | detail | other | Btn |
name | detail | other | Btn |
name | detail | other | Btn |
name | detail | other | Btn |
name | detail | other | Btn |
答案 0 :(得分:0)
我解决了!
按照这个post,这很简单。asp控件中的代码:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgbtn" runat="server" ImageUrl="../Image/btn.png" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
C#中的代码
gridView1.RowCreated += new GridViewRowEventHandler(gridView1_RowCreated);
...
void gridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
TableCell actionsCell = row.Cells[0];
row.Cells.Remove(actionsCell);
row.Cells.Add(actionsCell);
}
答案 1 :(得分:0)
我发现了问题。
我的原始代码:
var DT = new DataTable();
DT .Columns.Add("Name", typeof(string));//it should place in front of the btn column
DT .Columns.Add("Detail", typeof(string));
DT .Columns.Add("other", typeof(string));
.....foreach loop
DataRow dr = DT .NewRow();
dr["Name"] = item["Name"];
dr["Detail"] = item["Detail"];
dr["other"] = item["other"];
waitDT.Rows.Add(dr);
.....end of foreach loop
GridView1.DataSource = DT;
GridView1.DataBind();
问题是我使用var来声明dataTable。我不知道它的声明类型,但是,当我使用DataTable
声明它时,一切运行良好!
DataTable DT = new DataTable();
答案 2 :(得分:-1)
尝试在gridview上将AutoGenerateProperty设置为false。
答案 3 :(得分:-1)
aspx中的代码
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Detail" HeaderText="Detail" />
<asp:BoundField DataField="other" HeaderText="other" />
<asp:TemplateField >
<ItemTemplate>
<asp:Button ID="buttonReject" Text="Reject" runat="server" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" />
</asp:TemplateField>
</Columns>
</asp:GridView>
背后的代码
foreach (....)
{
DataRow dr = DT.NewRow();
dr["Name"] = item["Name"];
dr["Detail"] = item["Detail"];
dr["other"] = item["other"];
DT.Rows.Add(dr);
}
GridView1.DataSource = DT;
GridView1.DataBind();