使用数据向gridview添加新coloumn。 [英] adding new coloumn to gridview with data.

查看:68
本文介绍了使用数据向gridview添加新coloumn。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好



我正在开展其他开发人员之前制作的电子考勤项目。

客户需要一些改动。 />
这是一个项目,将从生物识别机器提交的数据库中检索出勤细节。



现在在一个页面中显示员工及时的时间其他数据按升序排列进行修改。检索通过存储过程进行。



我的问题是如何添加新列,其中包含通过相同存储过程检索的数据(out-time)而不更改以前的代码。该行将添加到两个之间其他栏目。



(夏季 - 如何在我想要的位置添加列到网格视图.. !!)



问候

hello

I am working on a e-attendance project made before by other developers.
client need some changes.
its a project that will retrieve attendance details from database submitted by biometric machine.

Now in a page its showing in-time of employee with some other data in ascending order for modification. retrieve takes place through stored procedure.

my question is how to add new column with data (out-time) retrieved through same store procedure without changing the previous codes.the row will b added in between two other columns.

(summery- how to add column to grid view at my desired location..!!)

regards

推荐答案

你好dibyaaryan007,



如果你不确定在哪里添加一个新的列到网格,然后我建议你使用下面的类在gridview中创建一个TemplateField。请查看下面的代码。





Hi dibyaaryan007,

If you are not sure where to add a new column to the grid, then I wold suggest you to use below class to create a TemplateField in your gridview. Please look at the below code for example.


// This is a class DynamicGridViewTextTemplate which inherits ITemplate interface to create a new column for a gridview

public class DynamicGridViewTextTemplate : ITemplate
{
	string _ColName;
	DataControlRowType _rowType;
	int _Count;


	public DynamicGridViewTextTemplate(string ColName, DataControlRowType RowType)
	{
		_ColName = ColName;
		_rowType = RowType;
	}

	public DynamicGridViewTextTemplate(DataControlRowType RowType, int ArticleCount)
	{
		_rowType = RowType;
		_Count = ArticleCount;
	}

	public void InstantiateIn(System.Web.UI.Control container)
	{
		switch (_rowType)
		{
			case DataControlRowType.Header:
				Label lc = new Label();
				lc.Text = _ColName;
				container.Controls.Add(lc);
				break;
			case DataControlRowType.DataRow:
				Label lbl = new Label();
				lbl.DataBinding += new EventHandler(this.lbl_DataBind);
				container.Controls.Add(lbl);
				break;
			case DataControlRowType.Footer:
				Label flc = new Label();
				container.Controls.Add(flc);
				break;
			default:
				break;
		}
	}

	private void lbl_DataBind(Object sender, EventArgs e)
	{
		Label lbl = (Label)sender;
		GridViewRow row = (GridViewRow)lbl.NamingContainer;
		lbl.Text = DataBinder.Eval(row.DataItem, _ColName).ToString();
	}
}





然后,下面的代码用于在绑定数据之前向网格添加一列。





Then below code is used to add a column to the grid before binding the data.

// Adds extra  columns to gridview, here Im adding 0th column of datatable at XXXXth column of  the gridview
private void AddColumns(DataTable dt)
{
	TemplateField tf;
	for (int i = 0; i < dt.Rows.Count; i++)
	{ 
            tf = new TemplateField();
	    tf.HeaderText = "Col Header";
	    tf.HeaderTemplate = new DynamicGridViewTextTemplate(Convert.ToString(dt.Rows[i][0]), DataControlRowType.Header);
	    tf.HeaderStyle.CssClass = "gridHeader";
	    tf.ItemTemplate = new DynamicGridViewTextTemplate(Convert.ToString(dt.Rows[i][0]), DataControlRowType.DataRow);
	    gridView.Columns.Insert(XXXX, tf);
	}
}







XXXX 指定必须添加新动态列的列号。



请仔细阅读代码以便更好地理解。为了实现这一点,你将视图状态设置为gridview的错误(EnableViewState =false)。



谢谢,

Vamsi




XXXX specifies the column number at which the new dynamic column has to be added.

Please go through the code for a better understanding. For this to be achieved, you''ve make the viewstate as false for the gridview (EnableViewState="false").

Thank you,
Vamsi


1。在GridView MarkUp中的所需位置添加一个新列。如下所示。

1. Add one new column in GridView MarkUp at desired location. Something like below.
<asp:Boundfield DataField="OutTimeInQuery" HeaderText="Out Time" />



这里 DataField OutTimeInQuery表示select语句中选定的列名,即数据库列名。



2.通过在存储过程中添加该列来获取该列select statement。


Here the DataField "OutTimeInQuery" indicates the selected column name in select statement, that is the database column name.

2. Fetch that from Store Procedure by add that column in the select statement.


这篇关于使用数据向gridview添加新coloumn。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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