如何在gridview中添加列? [英] How can add a column in gridview?

查看:62
本文介绍了如何在gridview中添加列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个如下的gridview,

Hi,

i have a gridview as below,

< <asp:GridView ID="overspeedReportGrid"  runat="server"  Width="100%" RowStyle-talAlign="Left">
<columns>
 <asp:BoundField DataField="DATE" HeaderText="Date" ReadOnly="True"  HeaderStyle-HorizontalAlign="Left"/>

<asp:BoundField DataField="VEHICLE_NUMBER" HeaderText="Vehicle no" ReadOnly="True"  HeaderStyle-HorizontalAlign="Left" />

 <asp:BoundField DataField="ALLOWED_SPEED" HeaderText="Maximum allowed speed"  HeaderStyle-HorizontalAlign="Left"/>
 <asp:BoundField DataField="OVERSPEED" HeaderText="Overspeed"  HeaderStyle-HorizontalAlign="Left"/>
 </columns>




但是,我想在运行时添加一个名为"Location"的新列,并向
添加值 隐藏字段中的每个位置"列.

在我的数据源中不包含``位置''列.我想从
填充它 从隐藏字段动态添加值.



我该怎么办?

请帮助......

谢谢..




But, i want to add a new column called ''Location'' at runtime and add value to the
each Location column from a hidden field.

In my data source not contain the column ''Location''.I wish to fill it from
dynamically added values from hidden field.



how can i do this?

plz help....

Thanks..

推荐答案

我建​​议您静态添加模板列,就像在此处声明gridview一样.
I''ll suggest you to add a template column statically, like you are declaring the gridview here.
<asp:templatefield visible="false" >
   <itemtemplate>
       <asp:hiddenfield runat="server" id="Location" />
   </itemtemplate>
</asp:templatefield>


现在,您可以找到每行的隐藏字段并将其添加到其中.

希望您能理解,并且对您有用.
--Amit


Now you can find the hidden field for each row and add the value to it.

Hope you understood and it works for you.
--Amit


以下链接可能对您有帮助
在运行时在gridview中添加边界域列 [ ^ ]
使用运行时生成的列创建GridView [ ^ ]
The below links might help you
Add boundfield column in gridview at runtime[^]
Creating a GridView with columns generated at runtime[^]


Hello Hasbina,

您必须将列动态添加到数据表中

例如:

DAtaTable dt = new DAtaTable();
dt.Columns.Add("DATE");
dt.Columns.Add("VEHICLE_NUMBER");
dt.Columns.Add("ALLOWED_SPEED");
dt.Columns.Add("OVERSPEED");
dt.Columns.Add("Location");
DataRow dr = dt.NewRow;
dr("DATE")= 1
dr("VEHICLE_NUMBER")="1234"
dr("ALLOWED_SPEED")="90"
dr("OVERSPEED")="100"
dr("Location")="Mohali"
dt.Rows.Add(dr);

GridView自动生成的列为true

GridView1.AutoGenerateColumns = True
GridView1.DataSource = dt
GridView1.DataBind()


您可以在rowdatabound事件中将样式添加到gridview


您还可以将列动态添加到网格

foreach(dt.Columns中的DataColumn col)
{
BoundField字段= new BoundField();
field.DataField = col.ColumnName;
field.HeaderText = col.ColumnName;
GridView1.Columns.Add(field);
}
Hello Hasbina,

you have to add column dynamically to the datatable

for ex:

DAtaTable dt=new DAtaTable();
dt.Columns.Add("DATE");
dt.Columns.Add("VEHICLE_NUMBER");
dt.Columns.Add("ALLOWED_SPEED");
dt.Columns.Add("OVERSPEED");
dt.Columns.Add("Location");
DataRow dr=dt.NewRow;
dr("DATE") = 1
dr("VEHICLE_NUMBER") = "1234"
dr("ALLOWED_SPEED") = "90"
dr("OVERSPEED") = "100"
dr("Location") = "Mohali"
dt.Rows.Add(dr);

GridView Autogenerated Columns to true

GridView1.AutoGenerateColumns = True
GridView1.DataSource = dt
GridView1.DataBind()


you can add the style to gridview in rowdatabound event


You can also add columns dynamically to grid

foreach (DataColumn col in dt.Columns)
{
BoundField field = new BoundField();
field.DataField = col.ColumnName;
field.HeaderText = col.ColumnName;
GridView1.Columns.Add(field);
}


这篇关于如何在gridview中添加列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆