Click here to Skip to main content
15,886,795 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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
Posted
Comments
Mayur Panchal 24-May-13 2:52am    
show us code for that and stotedproc.
walterhevedeich 24-May-13 3:26am    
First, just add the column on the select query of your stored procedure. The issue now of whether there will be code changes will depend on how the data is bound to the Gridview control. But I doubt there won't be a code change. :)

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.


C#
// 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.

C#
// 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 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
 
Share this answer
 
v2
1. Add one new column in GridView MarkUp at desired location. Something like below.
ASP.NET
<asp:Boundfield DataField="OutTimeInQuery" HeaderText="Out Time" />

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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900