I am really having trouble adding a template field to a RADGRID in my cs file. Here is what I have:
public void BindData() |
{ |
//Default.PK; |
//int theUser; |
//string email = Environment.UserName + "recyclingit.com"; |
string sqlConnection = "Data Source=DESQL2008;Initial Catalog=DrawOnTime;User Id=ontimeweb;Password=ontime1234;", query; |
query="select IncidentId, IncidentNumber, ReportedDate, Users.FirstName + ' ' + Users.LastName as 'IT Employee Assigned', StartDate, CompletionDate, WorkflowSteps.StepName" + |
" from incidents left join Users on Users.UserId=Incidents.AssignedToId left join WorkflowSteps on WorkflowSteps.WorkflowStepId=Incidents.WorkflowStepId where Incidents.ReportedByCustomerContactId = @PK"; |
SqlConnection dbconn = new SqlConnection(sqlConnection); |
dbconn.Open(); |
SqlCommand myCommand = new SqlCommand(query, dbconn); |
//myCommand.CommandType = CommandType.Text; |
//SqlCommand secondCommand = new SqlCommand("select CustomerContactId from CustomerContacts where Email = @email", dbconn); |
//secondCommand.CommandType = CommandType.Text; |
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand); |
DataSet ds = new DataSet(); |
myCommand.Parameters.AddWithValue("PK", Default.PK); |
myAdapter.Fill(ds); |
//sqlquery.Parameters.AddWithValue("email", email); |
myCommand.ExecuteScalar(); |
RadGrid1.DataSource = ds; |
RadGrid1.DataBind(); |
TemplateColumn workFlow = new TemplateColumn(); |
RadGrid1.Columns.Add(workFlow); |
dbconn.Close(); |
} |
5 Answers, 1 is accepted
0

Princy
Top achievements
Rank 2
answered on 19 Aug 2009, 04:20 AM
Hi Nate,
Here is a help article which explains how to create template columns dynamically. Go through the section entitled
'Creating template columns programmatically'.
http://www.telerik.com/help/aspnet-ajax/grdprogrammaticcreation.html
Princy.
Here is a help article which explains how to create template columns dynamically. Go through the section entitled
'Creating template columns programmatically'.
http://www.telerik.com/help/aspnet-ajax/grdprogrammaticcreation.html
Princy.
0

Nate
Top achievements
Rank 1
answered on 24 Aug 2009, 05:00 PM
Okay so I tried implementing that code, but I seem to be doing something wrong. I have a grid already placed on the aspx page that is not binded to anything until the page_init happens where I bind it progrmmatically. All I want to do is add a column that contains a drop down list binded to data I set programmatically, but I do not even see the new column in my grid with this code. I used the example given by Telerik on how to add a column programmatically, but I cannot see the new column. Thanks in advance for your help! Here is my code:
protected void Page_Init(object sender, EventArgs e) |
{ |
if (!Page.IsPostBack) |
{ |
BindData(); |
} |
public void BindData() |
{ |
//Default.PK; |
//int theUser; |
//string email = Environment.UserName + "recyclingit.com"; |
string sqlConnection = "Data Source=DESQL2008;Initial Catalog=DrawOnTime;User Id=ontimeweb;Password=ontime1234;", query; |
query = "select IncidentId, IncidentNumber, ReportedDate, Users.FirstName + ' ' + Users.LastName as 'IT Employee Assigned', StartDate, CompletionDate, WorkflowSteps.StepName" + |
" from incidents left join Users on Users.UserId=Incidents.AssignedToId left join WorkflowSteps on WorkflowSteps.WorkflowStepId=Incidents.WorkflowStepId where Incidents.ReportedByCustomerContactId = @PK"; |
SqlConnection dbconn = new SqlConnection(sqlConnection); |
dbconn.Open(); |
SqlCommand myCommand = new SqlCommand(query, dbconn); |
//myCommand.CommandType = CommandType.Text; |
//SqlCommand secondCommand = new SqlCommand("select CustomerContactId from CustomerContacts where Email = @email", dbconn); |
//secondCommand.CommandType = CommandType.Text; |
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand); |
DataSet ds = new DataSet(); |
myCommand.Parameters.AddWithValue("PK", Default.PK); |
myAdapter.Fill(ds); |
//sqlquery.Parameters.AddWithValue("email", email); |
//myCommand.ExecuteScalar(); |
RadGrid1.DataSource = ds; |
RadGrid1.DataBind(); |
//TemplateColumn workFlow = new TemplateColumn(); |
//RadGrid1.Columns.Add(workFlow); |
dbconn.Close(); |
string templateColumnName = "ContactName"; |
GridTemplateColumn templateColumn = new GridTemplateColumn(); |
templateColumn.ItemTemplate = new MyTemplate(templateColumnName); |
templateColumn.HeaderText = templateColumnName; |
GridBoundColumn boundColumn1 = new GridBoundColumn(); |
boundColumn1.DataField = "ContactName"; |
boundColumn1.UniqueName = "ConactName"; |
boundColumn1.HeaderText = "Bound Column"; |
RadGrid1.MasterTableView.Columns.Add(templateColumn); |
RadGrid1.MasterTableView.Columns.Add(boundColumn1); |
} |
private class MyTemplate : ITemplate |
{ |
protected LiteralControl lControl; |
protected RequiredFieldValidator validatorTextBox; |
protected HyperLink searchGoogle; |
protected TextBox textBox; |
protected CheckBox boolValue; |
private string colname; |
public MyTemplate(string cName) |
{ |
colname = cName; |
} |
public void InstantiateIn(System.Web.UI.Control container) |
{ |
lControl = new LiteralControl(); |
lControl.ID = "lControl"; |
lControl.DataBinding += new EventHandler(lControl_DataBinding); |
textBox = new TextBox(); |
textBox.ID = "templateColumnTextBox"; |
validatorTextBox = new RequiredFieldValidator(); |
validatorTextBox.ControlToValidate = "templateColumnTextBox"; |
validatorTextBox.ErrorMessage = "*"; |
searchGoogle = new HyperLink(); |
searchGoogle.ID = "searchGoogle"; |
searchGoogle.DataBinding += new EventHandler(searchGoogle_DataBinding); |
boolValue = new CheckBox(); |
boolValue.ID = "boolValue"; |
boolValue.DataBinding += new EventHandler(boolValue_DataBinding); |
boolValue.Enabled = false; |
Table table = new Table(); |
TableRow row1 = new TableRow(); |
TableRow row2 = new TableRow(); |
TableCell cell11 = new TableCell(); |
TableCell cell12 = new TableCell(); |
TableCell cell21 = new TableCell(); |
TableCell cell22 = new TableCell(); |
row1.Cells.Add(cell11); |
row1.Cells.Add(cell12); |
row2.Cells.Add(cell21); |
row2.Cells.Add(cell22); |
table.Rows.Add(row1); |
table.Rows.Add(row2); |
cell11.Text = colname + ": "; |
cell12.Controls.Add(lControl); |
cell21.Text = "Search Google for: "; |
cell22.Controls.Add(searchGoogle); |
container.Controls.Add(textBox); |
container.Controls.Add(validatorTextBox); |
container.Controls.Add(table); |
container.Controls.Add(new LiteralControl("<br />")); |
container.Controls.Add(boolValue); |
} |
void boolValue_DataBinding(object sender, EventArgs e) |
{ |
CheckBox cBox = (CheckBox)sender; |
GridDataItem container = (GridDataItem)cBox.NamingContainer; |
cBox.Checked = (bool)((DataRowView)container.DataItem)["Bool"]; |
} |
void searchGoogle_DataBinding(object sender, EventArgs e) |
{ |
HyperLink link = (HyperLink)sender; |
GridDataItem container = (GridDataItem)link.NamingContainer; |
link.Text = ((DataRowView)container.DataItem)[colname].ToString(); |
link.NavigateUrl = "http://www.google.com/search?hl=en&q=" + |
((DataRowView)container.DataItem)["ContactName"].ToString() + |
"&btnG=Google+Search"; |
} |
public void lControl_DataBinding(object sender, EventArgs e) |
{ |
LiteralControl l = (LiteralControl)sender; |
GridDataItem container = (GridDataItem)l.NamingContainer; |
l.Text = ((DataRowView)container.DataItem)[colname].ToString() + "<br />"; |
} |
} |
} |
} |
0
Hi Nate ,
If you want to add GridTemplateColumn dynamically, you should create the RadGrid entirely in the code-behind in the Page_Init event handler.
Please try it and tell me whether it works for your case.
Greetings,
Mira
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
If you want to add GridTemplateColumn dynamically, you should create the RadGrid entirely in the code-behind in the Page_Init event handler.
Please try it and tell me whether it works for your case.
Greetings,
Mira
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0

Nate
Top achievements
Rank 1
answered on 14 Sep 2009, 07:50 PM
So I had the default table show up on my page using this code, but I am trying to bind it to data I am bringing in from a sql server table. Basically the only change I made is that I use the boundcolumn.datarow= "IncidentNumber" from the database. Is this a correct approach to binding it?
RadGrid grid = new RadGrid(); |
grid.AutoGenerateColumns = false; |
//grid.DataSourceID = "SqlDataSource1"; |
string sqlConnection = "Data Source=DESQL2008;Initial Catalog=DrawOnTime;User Id=ontimeweb;Password=ontime1234;", query; |
query = "select IncidentId, IncidentNumber, ReportedDate, Users.FirstName + ' ' + Users.LastName as 'IT Employee Assigned', StartDate, CompletionDate, WorkflowSteps.StepName" + |
" from incidents left join Users on Users.UserId=Incidents.AssignedToId left join WorkflowSteps on WorkflowSteps.WorkflowStepId=Incidents.WorkflowStepId where Incidents.ReportedByCustomerContactId = @PK"; |
SqlConnection dbconn = new SqlConnection(sqlConnection); |
dbconn.Open(); |
SqlCommand myCommand = new SqlCommand(query, dbconn); |
//myCommand.CommandType = CommandType.Text; |
//SqlCommand secondCommand = new SqlCommand("select CustomerContactId from CustomerContacts where Email = @email", dbconn); |
//secondCommand.CommandType = CommandType.Text; |
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand); |
DataSet ds = new DataSet(); |
myCommand.Parameters.AddWithValue("PK", Default.PK); |
myAdapter.Fill(ds); |
//sqlquery.Parameters.AddWithValue("email", email); |
grid.DataSource = ds; |
grid.DataBind(); |
string templateColumnName = "ContactName"; |
GridTemplateColumn templateColumn = new GridTemplateColumn(); |
templateColumn.EditItemTemplate = new MyTemplate(templateColumnName); |
templateColumn.HeaderText = templateColumnName; |
GridBoundColumn boundColumn1 = new GridBoundColumn(); |
boundColumn1.DataField = "ContactName"; |
boundColumn1.UniqueName = "ConactName"; |
boundColumn1.HeaderText = "Bound Column"; |
boundColumn1.DataField = "IncidentNumber"; |
grid.MasterTableView.Columns.Add(templateColumn); |
grid.MasterTableView.Columns.Add(boundColumn1); |
grid.AllowPaging = true; |
grid.PageSize = 3; |
grid.Skin = "Outlook"; |
PlaceHolder1.Controls.Add(grid); |
dbconn.Close(); |
//BindData(); |
0
Hi Nate ,
Your code snippet seems fine to me. Is the grid working fine and if not, what is the unexpected behavior?
Regards,
Mira
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Your code snippet seems fine to me. Is the grid working fine and if not, what is the unexpected behavior?
Regards,
Mira
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.