I am unable to get sorting to work for this grid as well as to control the size of the filter text boxes for this grid.
I think the problem is associated with the way I select the datasource but I do not know how to fix it and I need to allow selecting the Sql table to the user.
If I try to use the Item_created event to control the size of the filter text boxes, I get a null reference exception.
And sorting is not working. When I click on the header of the column I can see the small arrow changing up or down but nothing happens.
How can I get control of the filter text boxes size and make the sorting working in this context?
Any help will be appreciated.
I think the problem is associated with the way I select the datasource but I do not know how to fix it and I need to allow selecting the Sql table to the user.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.DataSource = SqlDataSource2;
DropDownList1.DataBind();
}
SetDataSource();
}
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex > 0)
{
SetDataSource();
RadGrid1.Rebind();
}
}
private void SetDataSource()
{
if (DropDownList1.SelectedIndex > 0)
{
string tableInUse = DropDownList1.SelectedItem.Text;
RadGrid1.DataSource = SqlDataSource1;
SqlDataSource1.DeleteCommand = "DELETE FROM [" + tableInUse + "] WHERE [Id] = @Id";
SqlDataSource1.InsertCommand = "INSERT INTO [" + tableInUse + "] ([Code], [Description], [Bkgroup], [Stgroup], [Quantity], [BomNote], [UnitEng], [AlternCost]) VALUES (@Code, @Description, @Bkgroup, @Stgroup, @Quantity, @BomNote, @UnitEng, @AlternCost)";
SqlDataSource1.SelectCommand = "SELECT prod.Id, prod.Code, prod.Description, prod.Bkgroup, prod.Stgroup, prod.Quantity," +
"prod.BomNote, prod.UnitEng, prod.AlternCost, comp.Unit, comp.Cost, comp.NoteComp " +
"FROM " + tableInUse + " prod " +
"left JOIN Components comp ON comp.Code = prod.Code";
SqlDataSource1.UpdateCommand = "UPDATE [" + tableInUse + "] SET [Code] = @Code, [Description] = @Description, [Bkgroup] = @Bkgroup, [Stgroup] = @Stgroup, [Quantity] = @Quantity, [BomNote] = @BomNote, [UnitEng] = @UnitEng, [AlternCost] = @AlternCost WHERE [Id] = @Id";
}
}
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource = SqlDataSource1;
}
protected void Button1_Click(object sender, EventArgs e)
{
RadGrid1.MasterTableView.TableLayout = GridTableLayout.Auto;
RadGrid1.AllowFilteringByColumn = true;
RadGrid1.MasterTableView.AllowFilteringByColumn = true;
RadGrid1.Rebind();
}
protected void Button2_Click(object sender, EventArgs e)
{
RadGrid1.MasterTableView.TableLayout = GridTableLayout.Auto;
RadGrid1.AllowFilteringByColumn = false;
RadGrid1.MasterTableView.AllowFilteringByColumn = false;
RadGrid1.Rebind();
}
protected void RadGrid1_ItemCreated1(object sender, GridItemEventArgs e)
{
if (e.Item is GridFilteringItem)
{
GridFilteringItem filteringItem = e.Item as GridFilteringItem;
//set dimensions for the filter textbox
TextBox box = filteringItem["Code"].Controls[0] as TextBox;
box.Width = Unit.Pixel(30);
}
}
If I try to use the Item_created event to control the size of the filter text boxes, I get a null reference exception.
<
div
>
<
asp:DropDownList
ID
=
"DropDownList1"
runat
=
"server"
AutoPostBack
=
"True"
AppendDataBoundItems
=
"true"
OnSelectedIndexChanged
=
"DropDownList1_SelectedIndexChanged1"
DataTextField
=
"Name"
DataValueField
=
"Name"
>
<
asp:ListItem
Text
=
"-Select a BOM"
/>
</
asp:DropDownList
>
<
asp:SqlDataSource
ID
=
"SqlDataSource2"
runat
=
"server"
ConnectionString="<%$ ConnectionStrings:BOMConnectionString %>" SelectCommand="SELECT [Name] FROM [IndexTabb]"></
asp:SqlDataSource
>
<
asp:Button
ID
=
"Button1"
runat
=
"server"
OnClick
=
"Button1_Click"
Text
=
"Filter On"
/>
<
asp:Button
ID
=
"Button2"
runat
=
"server"
OnClick
=
"Button2_Click"
Text
=
"Filter Off"
/>
<
br
/>
<
br
/>
<
br
/>
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
Culture
=
"it-IT"
AllowPaging
=
"True"
AllowSorting
=
"True"
AllowAutomaticDeletes
=
"True"
AllowAutomaticInserts
=
"True"
AllowAutomaticUpdates
=
"True"
AutoGenerateDeleteColumn
=
"True"
AutoGenerateEditColumn
=
"True"
OnNeedDataSource
=
"RadGrid1_NeedDataSource"
OnItemCreated
=
"RadGrid1_ItemCreated1"
>
<
ExportSettings
ExportOnlyData
=
"True"
>
<
Pdf
PageWidth
=
""
>
</
Pdf
>
</
ExportSettings
>
<
ClientSettings
>
<
Scrolling
AllowScroll
=
"True"
UseStaticHeaders
=
"True"
/>
</
ClientSettings
>
<
MasterTableView
CommandItemDisplay
=
"Top"
DataKeyNames
=
"Code"
InsertItemPageIndexAction
=
"ShowItemOnCurrentPage"
AllowNaturalSort
=
"False"
>
<
CommandItemSettings
ShowExportToExcelButton
=
"True"
/>
<
Columns
>
<
telerik:GridEditCommandColumn
ButtonType
=
"ImageButton"
>
<
HeaderStyle
Width
=
"30px"
/>
</
telerik:GridEditCommandColumn
>
<
telerik:GridButtonColumn
ButtonType
=
"ImageButton"
Text
=
"Delete"
CommandName
=
"Delete"
FilterControlAltText
=
"Filter column1 column"
ConfirmDialogType
=
"RadWindow"
ConfirmText
=
"Do you really want to delete this project and all its content?"
UniqueName
=
"Cancel"
>
<
HeaderStyle
Width
=
"30px"
/>
</
telerik:GridButtonColumn
>
</
Columns
>
<
EditFormSettings
>
<
EditColumn
UniqueName
=
"EditCommandColumn1"
FilterControlAltText
=
"Filter EditCommandColumn1 column"
></
EditColumn
>
</
EditFormSettings
>
<
PagerStyle
AlwaysVisible
=
"True"
/>
</
MasterTableView
>
<
PagerStyle
AlwaysVisible
=
"True"
/>
</
telerik:RadGrid
>
<
br
/>
<
br
/>
</
div
>
<
asp:SqlDataSource
ID
=
"SqlDataSource1"
runat
=
"server"
ConnectionString="<%$ ConnectionStrings:BOMConnectionString %>">
</
asp:SqlDataSource
>
How can I get control of the filter text boxes size and make the sorting working in this context?
Any help will be appreciated.