在GridView中插入列 [英] Inserting Column in GridView
本文介绍了在GridView中插入列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Binding()GridView之后,我想在某些特定位置插入列.
after Binding() GridView i want to insert column at some specific location.
gv1.DataSource = ds.Tables[0];
gv1.DataBind();
gv1.Visible = true;
TemplateField tf = new TemplateField();
gv1.Columns.Insert(3, tf);
当表中的列多于3
it throws exception (out of bound) while columns in table is more then 3
推荐答案
时,它会引发异常(超出范围)网格上的方法.
以下是一些示例代码,用于添加模板字段和datacontrolfield
将此代码添加到您的页面,它应该将列添加到您的网格中
祝你好运
I believe the issue is that you need to add the column before you call the databind method on the grid.
Below is some sample code to add a template field and also a datacontrolfield
Add this code to your page and it should add the columns to you grid
Good Luck
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
LoadData()
End Sub
Private Sub LoadData()
Dim pi As New PersonInfo
Dim listOfPI As New List(Of PersonInfo)
pi.Name = "JOEY"
pi.Password = "Password01"
pi.Phone = "555-555-5555"
pi.Gender = "Male"
listOfPI.Add(pi)
pi = New PersonInfo
pi.Name = "Johnny"
pi.Password = "Password02"
pi.Phone = "555-555-6666"
pi.Gender = "Male"
listOfPI.Add(pi)
pi = New PersonInfo
pi.Name = "Jane"
pi.Password = "Password03"
pi.Phone = "555-555-7777"
pi.Gender = "Female"
listOfPI.Add(pi)
pi = New PersonInfo
pi.Name = "Joannie"
pi.Password = "Password03"
pi.Phone = "555-555-8888"
pi.Gender = "Female"
listOfPI.Add(pi)
Dim ngcf As New NewgvColumnField
Dim dcf As DataControlField = ngcf.InsertNewColumn
dcf.HeaderText = "THIS IS THE NEW COLUMN"
dcf.Visible = True
dcf.ShowHeader = True
Me.GridView1.Columns.Insert(2, dcf)
Dim tf As New TemplateField
tf.HeaderText = "TEMPLATE FIELD"
tf.Visible = True
Me.GridView1.Columns.Insert(1, tf)
Me.GridView1.DataSource = listOfPI
Me.GridView1.DataBind()
End Sub
Public Class NewgvColumnField
Inherits DataControlField
Protected Overrides Function CreateField() As System.Web.UI.WebControls.DataControlField
Return New NewgvColumnField
End Function
Public Function InsertNewColumn() As DataControlField
Return CreateField()
End Function
End Class
Private Class PersonInfo
Property Name As String
Property Password As String
Property Phone As String
Property Gender As String
End Class
对不起,我忘记了您在c#中编写的内容
试试这个.
sorry i forgot that you wrote this in c#
Try this.
protected void // ERROR: Handles clauses are not supported in C#
Page_Load(object sender, System.EventArgs e)
{
LoadData();
}
private void LoadData()
{
PersonInfo pi = new PersonInfo();
List<PersonInfo> listOfPI = new List<PersonInfo>();
pi.Name = "JOEY";
pi.Password = "Password01";
pi.Phone = "555-555-5555";
pi.Gender = "Male";
listOfPI.Add(pi);
pi = new PersonInfo();
pi.Name = "Johnny";
pi.Password = "Password02";
pi.Phone = "555-555-6666";
pi.Gender = "Male";
listOfPI.Add(pi);
pi = new PersonInfo();
pi.Name = "Jane";
pi.Password = "Password03";
pi.Phone = "555-555-7777";
pi.Gender = "Female";
listOfPI.Add(pi);
pi = new PersonInfo();
pi.Name = "Joannie";
pi.Password = "Password03";
pi.Phone = "555-555-8888";
pi.Gender = "Female";
listOfPI.Add(pi);
NewgvColumnField ngcf = new NewgvColumnField();
DataControlField dcf = ngcf.InsertNewColumn;
dcf.HeaderText = "THIS IS THE NEW COLUMN";
dcf.Visible = true;
dcf.ShowHeader = true;
this.GridView1.Columns.Insert(2, dcf);
TemplateField tf = new TemplateField();
tf.HeaderText = "TEMPLATE FIELD";
tf.Visible = true;
this.GridView1.Columns.Insert(1, tf);
this.GridView1.DataSource = listOfPI;
this.GridView1.DataBind();
}
public class NewgvColumnField : DataControlField
{
protected override System.Web.UI.WebControls.DataControlField CreateField()
{
return new NewgvColumnField();
}
public DataControlField InsertNewColumn()
{
return CreateField();
}
}
Private Class PersonInfo
{
private string _name;
public string Name {
get { return _name; }
set { _name = value; }
}
private string _password;
public string Password {
get { return _password; }
set { _password = value; }
}
private string _phone;
public string Phone {
get { return _phone; }
set { _phone = value; }
}
private string _gender;
public string Gender {
get { return _gender; }
set { _gender = value; }
}
}
这篇关于在GridView中插入列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文