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.
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Creating a Custom ItemTemplate But Still Bind to DataSource

Thread ID:

Created:

Updated:

Platform:

Replies:

76485 Sep 9,2008 11:36 AM UTC Sep 29,2008 06:45 AM UTC ASP.NET Web Forms (Classic) 5
loading
Tags: GridGroupingControl
Andrew Martin
Asked On September 9, 2008 11:36 AM UTC

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?


Rajarajeswari S [Syncfusion]
Replied On 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



Andrew Martin
Replied On 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?



Rajarajeswari S [Syncfusion]
Replied On 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



Administrator [Syncfusion]
Replied On 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.


Rajarajeswari S [Syncfusion]
Replied On 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




CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;