We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Creating a Custom ItemTemplate But Still Bind to DataSource

Is it possible to add a Button to a each cell in a given column without also clearing the value of that cell, a value that was setting and binding a DataSource to the GridGroupingControl.

I created my own custom ITemplate implementation that I set to the GridGroupingControl.TableDescriptor.Columns[i].ItemTemplate property, and it adds a button to the Container passed into the InstantiateOn function, but the button is all that is there now. The text value that is typical there is gone.

So, is there anyway to keep the value while also adding my own custom controls to a cell?


5 Replies

RS Rajarajeswari S Syncfusion Team September 16, 2008 05:04 AM UTC

Hi Andrew,

Sorry for the delay in getting back to you.

Is it possible to add a Button to a each cell in a given column without also clearing the value of that cell?

Yes, it is possible to have ItemTemplates without clearing the value of the cell.

Actually while having Templates for a certain field, we can’t have the value of those cells. But we can overcome this by having a label as one of the Template control with the values of that cell. Please refer the below code snippet which illustrates this:








protected string GetValue(GridCell container)
{
string val;
Record rowRecord = container.Row.Record;
val = rowRecord.GetValue("City").ToString();
return val;
}

Please refer the sample from the below link, which illustrates this:


http://www.syncfusion.com/support/user/uploads/Samples_49e7241d.zip


Please let us know if this helps you out.

Regards,
Raji




AM Andrew Martin September 18, 2008 02:09 PM UTC

I guess I should have made my question a little clearer. I'm actually populating the columns, names and all, from data extracted from a database T runtime. I have to bind the Grid at runtime because the tables joined and the SQL statement is generated from options in the database.

I've created a class that inherits ITemplate. This class creates the Label that you created at design time. Subbing in "<%#GetValue(Container)%>" at runtime for Label.Text causes that string to displayed as a literal value. Is there anyway to make this work at runtime?




RS Rajarajeswari S Syncfusion Team September 22, 2008 07:54 PM UTC

Hi Andrew,

Please refer the below code snippet which will initiate the label control on code behind using ItemTemplates.

protected void GridGroupingControl1_QueryCellStyleInfo(object sender, GridTableCellStyleInfoEventArgs e)
{
//Look for the RecordField and AlternateField Cell.
if (e.TableCellIdentity.TableCellType == GridTableCellType.RecordFieldCell || e.TableCellIdentity.TableCellType == GridTableCellType.AlternateRecordFieldCell)
{
//Look for the Column Name "Column2" in the GridGroupingControl when it get rendered
if (e.TableCellIdentity.Column.Name == "City")
{
System.Web.UI.WebControls.Label lbl = new Label();
lbl.ID = "Label1";
lbl.Init += new EventHandler(lbl_Init);
System.Web.UI.WebControls.Button btn = new Button();
btn.ID = "Button1";
btn.Text = "Click";
//Here we adding the DropDownListBox programatically using ITemplate Interface
TemplateClass mytemp = new TemplateClass(lbl,btn);
e.TableCellIdentity.Column.ItemTemplate = mytemp;
}

}
}

void lbl_Init(object sender, EventArgs e)
{
//throw new NotImplementedException();
Label lbl = (Label)sender;
GridRow gridrow = (GridRow)lbl.Parent.Parent.Parent;
string city = gridrow.Record.GetValue("City").ToString();
lbl.Text = city;

}

Please refer the sample from the below link, which illustrates the above:

http://www.syncfusion.com/support/user/uploads/Sample_2d644dee.zip

Please let me know if this helps you out.

Regards,
Raji




AD Administrator Syncfusion Team September 25, 2008 05:55 PM UTC

That works perfectly, thanks. One final question, however: I now have a grid with multiple rows and columns, but I need only save certain cell values (the label.Text) to a table in another database. How do I specifically access the controls on each cell in order to get the value of a particular label at (row, col) position?

Thanks.



RS Rajarajeswari S Syncfusion Team September 29, 2008 06:45 AM UTC


Hi Andrew,

From the GridRow we can access the row id, using this we could access the particular cell value, and save it separately. Please refer the below code snippet which illustrates this:

void lbl_Init(object sender, EventArgs e)
{

Label lbl = (Label)sender;
GridRow gridrow = (GridRow)lbl.Parent.Parent.Parent;
string city = gridrow.Record.GetValue("City").ToString();
lbl.Text = city;
string uid = gridrow.UniqueID;
string[] rposition = uid.Split(new char[] { '^' });
string[] rowpos = rposition[1].Split(new char[] { '*' });
int rowid = Convert.ToInt32(rowpos[0]);
if (rowid % 2 == 0)
{
val.Add(lbl.Text);
}

}

The above snippet simply store the value for the column “City” whose row id’s are even numbers.

Please refer the modified sample from the below link:

http://www.syncfusion.com/support/user/uploads/CellValue_7a9cf156.zip

Please let me know if you have any other concerns.

Regards,
Raji




Loader.
Live Chat Icon For mobile
Up arrow icon