This is a migrated thread and some comments may be shown as answers.

get RadGrid Columns Width

5 Answers 102 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Anupriya
Top achievements
Rank 1
Anupriya asked on 08 Dec 2010, 03:43 PM

I have created Telerik Grid like this and item Bound Function also shown like as follows:

Please tell me if I resized the columns then how will I get the width of column present in RadGrid.Please find that telerik Auto Generated Column is set to true.



"

Namespace="Microsoft.SharePoint.WebControls" %>

<%@ Register Assembly="Telerik.Web.UI, Version=2010.2.826.35, Culture=neutral, PublicKeyToken=121fae78165ba3d4" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>

<asp:Table runat="server">

<asp:TableRow>

<asp:TableCell>

<telerik:RadGrid ID="gridForPreNewEdit2" AllowPaging="true" ShowFooter="false" runat="server" AutoGenerateColumns="true" GridLines="Both">

</telerik:RadGrid>

</asp:TableCell>

</asp:TableRow>

</asp:Table>

void grdViewForPreview_ItemDataBound(object sender, GridItemEventArgs e)

{

try

{

 

if (e.Item is GridDataItem)

{

GridDataItem item = e.Item as GridDataItem;

SPWeb web = SPContext.Current.Web;

SPList list = web.Lists[new Guid(ddlSrcList.SelectedValue)];

 

SPListItem itemCol = list.Items.Add();

 

var contex = SPContext.GetContext(this.Context, 0, list.ID, list.ParentWeb);

 

foreach (GridColumn column in item.OwnerTableView.RenderColumns)

{

if (column.HeaderText == "Delete")

{

ImageButton btnDeleteFor = new ImageButton();

btnDeleteFor.ID = column.UniqueName;

btnDeleteFor.Enabled = false;

item[column.HeaderText].Controls.Clear();

item[column.HeaderText].Controls.Add(btnDeleteFor);

}

else

{

foreach (ListItem lstItem in lbDisplayedCols.Items)

{

if (lstItem.Text.ToLower().Trim() == column.HeaderText.ToLower().Trim())

{

SPField listField = list.Fields[column.HeaderText];

 

if (listField.Type != SPFieldType.DateTime && listField.Type != SPFieldType.User)

{

Panel pnlForBFC = new Panel();

pnlForBFC.ID = "Field2_" + listField.InternalName;

BaseFieldControl bfc = listField.FieldRenderingControl;

bfc.ID = "Field_" + listField.InternalName;

 

bfc.ControlMode = SPControlMode.New;

 

bfc.EnableViewState = true;

bfc.RenderContext = contex;

bfc.ItemContext = contex;

bfc.IsValid = true;

bfc.Visible = true;

pnlForBFC.Controls.Add(bfc);

pnlForBFC.Enabled = false;

 

item[column.HeaderText].Controls.Clear();

item[column.HeaderText].Controls.Add(pnlForBFC);

}

else if (listField.Type == SPFieldType.User)

{

 

PeopleEditor peopleEditor = new PeopleEditor();

 

item[column.HeaderText].Controls.Clear();

item[column.HeaderText].Controls.Add(peopleEditor);

}

else if (listField.Type == SPFieldType.DateTime)

{

DateTimeControl dtControl = new DateTimeControl();

dtControl.ID = "Field_" + listField.InternalName;

dtControl.Enabled = false;

item[column.HeaderText].Controls.Clear();

item[column.HeaderText].Controls.Add(dtControl);

}

 

}

}

}

}

}

else if (e.Item is GridFooterItem)

{

GridFooterItem itemForFooter = e.Item as GridFooterItem;

foreach (ListItem chosenListCol in lbDisplayedCols.Items)

{

if (ViewState[chosenListCol.Text] != null)

{

Label lblForFooter = new Label();

lblForFooter.ID = "Field_" + chosenListCol.Text;

lblForFooter.Text = "0";

lblForFooter.Width = Unit.Percentage(100);

lblForFooter.BorderColor = Color.Black;

lblForFooter.BorderStyle = BorderStyle.Solid;

lblForFooter.BorderWidth = 1;

itemForFooter[chosenListCol.Text].Controls.Clear();

itemForFooter[chosenListCol.Text].Controls.Add(lblForFooter);

}

}

}


5 Answers, 1 is accepted

Sort by
0
Tsvetina
Telerik team
answered on 14 Dec 2010, 10:00 AM
Hello Anupriya,

You can access the columns width by using e.Column.HeaderStyle.Width in the ColumnCreated event of RadGrid which is thrown for each auto-generated GridColumn. Additionally, it is recommended to address columns by UniqueName (column.UniqueName) instead of HeaderText, since this is the safest way (the HeaderText that you use to access columns is different from the UniqueName of the column in Q3 2010, for example).

All the best,
Tsvetina
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Jesse
Top achievements
Rank 1
answered on 07 Jul 2014, 08:23 PM
It seems like I can only get the width for columns that I've specified the width for? This code gives me 0.0 except for the column I set the width for. I am executing this code in rgResults_ItemDataBound. Is that the wrong place to do it?

foreach (GridColumn column in rgResults.Columns)
{
    int w = column.HeaderStyle.Width.Value;
}
0
Pavlina
Telerik team
answered on 10 Jul 2014, 02:55 PM
Hi Jesse,

Indeed using the pointed code you can retrieve the column width of the columns that have specified width. You can retrieve the widths of all columns on client-side, store them in an object and store this object in a hidden field. Then you can access this field on server and get the desired widths.

Regards,
Pavlina
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Jesse
Top achievements
Rank 1
answered on 10 Jul 2014, 04:37 PM
Pavlina,

Thank you for your response. It is a good idea. Unfortunately, I haven't been able to do anything client side. I've tried this:

var grid = $find("<%=rgResults.ClientID%>");

And it always returns null. This was using a standard script manager. I thought maybe I needed to use a RadScriptManager instead to be able to do this, but that's giving me a NullReferenceException when I add that to the page. So I don't really have any way of accessing the RadGrid from the client side at this time.
0
Pavlina
Telerik team
answered on 15 Jul 2014, 08:52 AM
Hi Jesse,

I have replied your other ticket with the same question and you can see the answer here:
http://www.telerik.com/forums/get_mastertableview-not-working

If you need additional assistance I will ask you to continue our communication there.

Regards,
Pavlina
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Anupriya
Top achievements
Rank 1
Answers by
Tsvetina
Telerik team
Jesse
Top achievements
Rank 1
Pavlina
Telerik team
Share this question
or