I have a Telerik Grid that has a checkbox in each column that will show whether or not each item in the Grid is active. The DropDownList has values from the database, and depending on which value is selected, the checkboxes will turn on or off.
Ex: I choose Category A from the DropDownList and the first record in the Grid will have a checked box to show that record is associated with Category A.
Ex: I choose Category A from the DropDownList and the first record in the Grid will have a checked box to show that record is associated with Category A.
<
telerik:RadDropDownList
ID
=
"MarketDropDownList"
runat
=
"server"
DefaultMessage
=
"--Select Market--"
Width
=
"200px"
DataSourceID
=
"ddlDataSource"
DataTextField
=
"MarketName"
DataValueField
=
"MarketID"
>
</
telerik:RadDropDownList
>
<
asp:SqlDataSource
ID
=
"ddlDataSource"
runat
=
"server"
ConnectionString="<%$ ConnectionStrings:DBConnectingString %>" SelectCommand="Select DISTINCT MarketID, MarketName FROM vPanel_Market">
</
asp:SqlDataSource
>
<
telerik:RadGrid
ID
=
"radProductsGrid"
runat
=
"server"
AllowPaging
=
"true"
AllowSorting
=
"true"
AutoGenerateColumns
=
"false"
GridLines
=
"None"
OnNeedDataSource
=
"radProductsGrid_NeedDataSource"
OnSortCommand
=
"radProductsGrid_SortCommand"
PageSize
=
"100"
ShowGroupPanel
=
"false"
Skin
=
"Default"
>
<
MasterTableView
AllowCustomSorting
=
"true"
AllowMultiColumnSorting
=
"false"
AllowNaturalSort
=
"false"
AllowSorting
=
"true"
AutoGenerateColumns
=
"false"
Width
=
"100%"
>
<Columns
>
<
telerik:GridTemplateColumn
AllowFiltering
=
"true"
HeaderText
=
"Product ID"
ReadOnly
=
"true"
UniqueName
=
"productid"
>
<
HeaderTemplate
>
<
asp:CheckBox
ID
=
"headerCheck"
runat
=
"server"
onClick
=
"javascript:SelectDeselectAllCheckboxes(this);"
/>
</
HeaderTemplate
>
<
ItemTemplate
>
<%--this checkbox will populate based off of selected value in dropdown --%>
<
asp:CheckBox
ID
=
"ProductInMarket"
runat
=
"server"
/>
<asp:HiddenField ID="ProductID" runat="server" Value="ProductID" />
</
ItemTemplate
>
<
ItemStyle
VerticalAlign
=
"Middle"
Width
=
"60px"
/>
</
telerik:GridTemplateColumn
>
<
telerik:GridBoundColumn
AllowFiltering
=
"true"
DataField
=
"ItemNumber"
HeaderText
=
"Item Number"
ReadOnly
=
"true"
SortExpression
=
"ItemNumber"
UniqueName
=
"ItemNumber"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
AllowFiltering
=
"true"
DataField
=
"ProductName"
HeaderText
=
"Product Name"
ReadOnly
=
"true"
SortExpression
=
"ProductName"
UniqueName
=
"ProductName"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
AllowFiltering
=
"true"
DataField
=
"CategoryName"
HeaderText
=
"Category Name"
ReadOnly
=
"true"
SortExpression
=
"CategoryName"
UniqueName
=
"CategoryName"
>
</
telerik:GridBoundColumn
>
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
9 Answers, 1 is accepted
0
Princy
Top achievements
Rank 2
answered on 31 Oct 2013, 05:57 AM
Hi Jamie,
Please try the following code snippet to check the CheckBox in RadGrid row based on the selected DropDownListItem .Here I am comparing the selected value of a DropDownList to the text of a GridBoundColumn.
C#:
Thanks,
Princy.
Please try the following code snippet to check the CheckBox in RadGrid row based on the selected DropDownListItem .Here I am comparing the selected value of a DropDownList to the text of a GridBoundColumn.
C#:
protected
void
MarketDropDownList_SelectedIndexChanged(
object
sender, Telerik.Web.UI.DropDownListEventArgs e)
{
RadGrid RadGrid1 = (RadGrid)form1.FindControl(
"radProductsGrid"
);
foreach
(GridDataItem ditem
in
RadGrid1.MasterTableView.Items)
{
TableCell cell = (TableCell)ditem[
"uniqueid"
];
if
(cell.Text == e.Text)
// cross checking with dropdownlistitem to gridboundcolumn text
{
CheckBox check = (CheckBox)ditem.FindControl(
"ProductInMarket"
);
check.Checked =
true
;
}
}
}
Thanks,
Princy.
0
Jamie
Top achievements
Rank 2
answered on 31 Oct 2013, 01:39 PM
Thank you for the snippet. :) Can you tell me what form1 should be? It doesn't exist in the current context, probably because I don't have it declared on my page. What should I replace that with?
0
Jamie
Top achievements
Rank 2
answered on 31 Oct 2013, 02:46 PM
I added the code snippet and the OnSelectedIndexChanged="MarketDropDownList_SelectedIndexChanged" to the telerik dropdownlist, but when I debug, the method doesn't even get hit. What am I doing wrong here?
<
telerik:RadDropDownList
ID
=
"MarketDropDownList"
runat
=
"server"
DefaultMessage
=
"--Select Market--"
Width
=
"200px"
DataSourceID
=
"ddlDataSource"
DataTextField
=
"MarketName"
DataValueField
=
"MarketID"
OnSelectedIndexChanged
=
" MarketDropDownList_SelectedIndexChanged"
>
</
telerik:RadDropDownList
>
protected void MarketDropDownList_SelectedIndexChanged(object sender, DropDownListEventArgs e)
{
//populate checkboxes next to Products that are associated with the Market that has been selected
RadGrid radGrid = (RadGrid)Page.FindControl("radProductsGrid");
foreach(GridDataItem ditem in radGrid.MasterTableView.Items)
{
TableCell cell = (TableCell)ditem["uniqueid"];
if(cell.Text == e.Text) // cross checking with dropdownlistitem to gridboundcolumn text
{
CheckBox check = (CheckBox)ditem.FindControl("ProductInMarket");
check.Checked = true;
}
}
}
0
Princy
Top achievements
Rank 2
answered on 01 Nov 2013, 02:45 AM
Hi Jamie,
Please try setting the RadDropDownList property AutoPostBack to true.
ASPX:
Thanks,
Princy.
Please try setting the RadDropDownList property AutoPostBack to true.
ASPX:
<
telerik:RadDropDownList
ID
=
"MarketDropDownList"
runat
=
"server"
DefaultMessage
=
"--Select Market--"
Width
=
"200px"
DataTextField
=
"MarketName"
DataValueField
=
"MarketID"
DataSourceID
=
"ddlDataSource"
AutoPostBack
=
"true"
OnSelectedIndexChanged
=
"MarketDropDownList_SelectedIndexChanged"
>
</
telerik:RadDropDownList
>
Thanks,
Princy.
0
Jamie
Top achievements
Rank 2
answered on 01 Nov 2013, 03:40 PM
I still can't get the checkboxes to show anything. I can see that it is refreshing the page since the addition of AutoPostBack, but when I compare with the database, my top row should have a checked box, and it does not.
protected void MarketDropDownList_SelectedIndexChanged(object sender, DropDownListEventArgs e)
{
try
{
//populate checkboxes next to Products that are associated with the Market that has been selected
RadGrid radGrid = (RadGrid)cntrlPnlDisplayAllProducts.FindControl("radProductsGrid");
foreach (GridDataItem ditem in radGrid.MasterTableView.Items)
{
TableCell cell = (TableCell)ditem["ItemNumber"];
if (cell.Text == e.Text) // cross checking with dropdownlistitem to gridboundcolumn text
{
CheckBox check = (CheckBox)ditem.FindControl("ProductInMarketCheckBox");
check.Checked = true;
}
}
}
catch(Exception ex)
{
Forbin.Logging.Log.LogException(Forbin.Logging.PageType.ProductManage, Forbin.Logging.MessageType.Exception, ex.ToString());
UIUtils.ShowMessageToUser("OnErrorMesg", this.Page);
}
}
{
try
{
//populate checkboxes next to Products that are associated with the Market that has been selected
RadGrid radGrid = (RadGrid)cntrlPnlDisplayAllProducts.FindControl("radProductsGrid");
foreach (GridDataItem ditem in radGrid.MasterTableView.Items)
{
TableCell cell = (TableCell)ditem["ItemNumber"];
if (cell.Text == e.Text) // cross checking with dropdownlistitem to gridboundcolumn text
{
CheckBox check = (CheckBox)ditem.FindControl("ProductInMarketCheckBox");
check.Checked = true;
}
}
}
catch(Exception ex)
{
Forbin.Logging.Log.LogException(Forbin.Logging.PageType.ProductManage, Forbin.Logging.MessageType.Exception, ex.ToString());
UIUtils.ShowMessageToUser("OnErrorMesg", this.Page);
}
}
0
Jamie
Top achievements
Rank 2
answered on 01 Nov 2013, 03:55 PM
I still can't get the checkboxes to show anything. I can see that it is refreshing the page since the addition of AutoPostBack, but when I compare with the database, my top row should have a checked box, and it does not.
protected void MarketDropDownList_SelectedIndexChanged(object sender, DropDownListEventArgs e)
{
try
{
//populate checkboxes next to Products that are associated with the Market that has been selected
RadGrid radGrid = (RadGrid)cntrlPnlDisplayAllProducts.FindControl("radProductsGrid");
foreach (GridDataItem ditem in radGrid.MasterTableView.Items)
{
TableCell cell = (TableCell)ditem["ItemNumber"];
if (cell.Text == e.Text) // cross checking with dropdownlistitem to gridboundcolumn text
{
CheckBox check = (CheckBox)ditem.FindControl("ProductInMarketCheckBox");
check.Checked = true;
}
}
}
catch(Exception ex)
{
Forbin.Logging.Log.LogException(Forbin.Logging.PageType.ProductManage, Forbin.Logging.MessageType.Exception, ex.ToString());
UIUtils.ShowMessageToUser("OnErrorMesg", this.Page);
}
}
{
try
{
//populate checkboxes next to Products that are associated with the Market that has been selected
RadGrid radGrid = (RadGrid)cntrlPnlDisplayAllProducts.FindControl("radProductsGrid");
foreach (GridDataItem ditem in radGrid.MasterTableView.Items)
{
TableCell cell = (TableCell)ditem["ItemNumber"];
if (cell.Text == e.Text) // cross checking with dropdownlistitem to gridboundcolumn text
{
CheckBox check = (CheckBox)ditem.FindControl("ProductInMarketCheckBox");
check.Checked = true;
}
}
}
catch(Exception ex)
{
Forbin.Logging.Log.LogException(Forbin.Logging.PageType.ProductManage, Forbin.Logging.MessageType.Exception, ex.ToString());
UIUtils.ShowMessageToUser("OnErrorMesg", this.Page);
}
}
0
Princy
Top achievements
Rank 2
answered on 04 Nov 2013, 10:39 AM
Hi Jamie,
Unfortunately I couldn't replicate the issue at my end. Please have a look into the full code I tried which works fine at my end.
ASPX:
C#:
Thanks,
Princy.
Unfortunately I couldn't replicate the issue at my end. Please have a look into the full code I tried which works fine at my end.
ASPX:
<
telerik:RadDropDownList
ID
=
"MarketDropDownList"
runat
=
"server"
DefaultMessage
=
"--Select Market--"
Width
=
"200px"
DataTextField
=
"ShipName"
DataValueField
=
"ShipName"
DataSourceID
=
"ddlDataSource"
AutoPostBack
=
"true"
OnSelectedIndexChanged
=
"MarketDropDownList_SelectedIndexChanged"
>
</
telerik:RadDropDownList
>
<
asp:Panel
ID
=
"panel1"
runat
=
"server"
>
<
telerik:RadGrid
ID
=
"radProductsGrid"
runat
=
"server"
AllowPaging
=
"true"
AllowSorting
=
"true"
AutoGenerateColumns
=
"false"
GridLines
=
"None"
PageSize
=
"100"
ShowGroupPanel
=
"false"
Skin
=
"Default"
OnNeedDataSource
=
"radProductsGrid_NeedDataSource"
>
<
MasterTableView
AllowCustomSorting
=
"true"
AllowMultiColumnSorting
=
"false"
AllowNaturalSort
=
"false"
AllowSorting
=
"true"
AutoGenerateColumns
=
"false"
Width
=
"100%"
>
<
Columns
>
<
telerik:GridTemplateColumn
AllowFiltering
=
"true"
HeaderText
=
"OrderID"
ReadOnly
=
"true"
UniqueName
=
"OrderID1"
>
<
HeaderTemplate
>
<
asp:CheckBox
ID
=
"headerCheck"
runat
=
"server"
/>
</
HeaderTemplate
>
<
ItemTemplate
>
<
asp:CheckBox
ID
=
"ProductInMarket"
runat
=
"server"
/>
<
asp:HiddenField
ID
=
"ProductID"
runat
=
"server"
Value
=
"ProductID"
/>
</
ItemTemplate
>
<
ItemStyle
VerticalAlign
=
"Middle"
Width
=
"60px"
/>
</
telerik:GridTemplateColumn
>
<
telerik:GridBoundColumn
AllowFiltering
=
"true"
DataField
=
"OrderID"
HeaderText
=
"OrderID"
ReadOnly
=
"true"
SortExpression
=
"OrderID"
UniqueName
=
"OrderID"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
AllowFiltering
=
"true"
DataField
=
"CustomerID"
HeaderText
=
"CustomerID"
ReadOnly
=
"true"
SortExpression
=
"CustomerID"
UniqueName
=
"CustomerID"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
AllowFiltering
=
"true"
DataField
=
"ShipName"
HeaderText
=
"ShipName"
ReadOnly
=
"true"
SortExpression
=
"ShipName"
UniqueName
=
"ShipName"
>
</
telerik:GridBoundColumn
>
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
</
asp:Panel
>
<
asp:SqlDataSource
ID
=
"ddlDataSource"
runat
=
"server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [OrderID], [CustomerID], [ShipName], [IsTrue] FROM [Orders]">
</
asp:SqlDataSource
>
C#:
protected
void
MarketDropDownList_SelectedIndexChanged(
object
sender, Telerik.Web.UI.DropDownListEventArgs e)
{
try
{
foreach
(GridDataItem ditem
in
radProductsGrid.MasterTableView.Items)
{
TableCell cell = (TableCell)ditem[
"ShipName"
];
if
(cell.Text == e.Text)
// cross checking with dropdownlistitem to gridboundcolumn
{
CheckBox check = (CheckBox)ditem.FindControl(
"ProductInMarket"
);
check.Checked =
true
;
}
}
}
catch
(Exception ex)
{
Response.Write(ex.Message);
}
}
protected
void
radProductsGrid_NeedDataSource(
object
sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
String ConnString = ConfigurationManager.ConnectionStrings[
"NorthwindConnectionString"
].ConnectionString;
SqlConnection conn =
new
SqlConnection(ConnString);
SqlDataAdapter adapter =
new
SqlDataAdapter();
adapter.SelectCommand =
new
SqlCommand(
"SELECT OrderID, CustomerID, ShipName FROM Orders"
, conn);
DataTable myDataTable =
new
DataTable();
conn.Open();
try
{
adapter.Fill(myDataTable);
}
finally
{
conn.Close();
}
radProductsGrid.DataSource = myDataTable;
}
Thanks,
Princy.
0
Jamie
Top achievements
Rank 2
answered on 04 Nov 2013, 02:26 PM
In debug mode, I realized that the line cell.Text == e.Text will never be equal, that is why it isn't working. cell.Text equals the Item Number of the product, and e.Text = the category of the product. Those 2 values will never match. I need something that will check that the item number is associated with that category.
(Ex: MSC4102 == EMS is what I was trying to do.)
The Item Number is in the Products table, the MarketID and MarketName are in the Market table, and these are connected with a MarketMappings table holding the ProductID and the MarketID.
(Ex: MSC4102 == EMS is what I was trying to do.)
The Item Number is in the Products table, the MarketID and MarketName are in the Market table, and these are connected with a MarketMappings table holding the ProductID and the MarketID.
0
Princy
Top achievements
Rank 2
answered on 05 Nov 2013, 03:57 AM
Hi Jamie,
Please try the following steps to achieve your scenario.
1. On RadDropDownList SelectedIndexChanged event, write an SQL query to get the 'ProductID' from the 'MarketMappings' Table based on 'MarketID'(selectedvalue of DropDownList).
2. Loop through each RadGrid row and compare the DataKeyValue(ProductID) of RadGrid with the ProductID that we got from the 1st step. If there is a match select the corresponding RadGrid Row. Please see the below code.
C#:
Thanks,
Princy.
Please try the following steps to achieve your scenario.
1. On RadDropDownList SelectedIndexChanged event, write an SQL query to get the 'ProductID' from the 'MarketMappings' Table based on 'MarketID'(selectedvalue of DropDownList).
2. Loop through each RadGrid row and compare the DataKeyValue(ProductID) of RadGrid with the ProductID that we got from the 1st step. If there is a match select the corresponding RadGrid Row. Please see the below code.
C#:
protected
void
MarketDropDownList_SelectedIndexChanged(
object
sender, Telerik.Web.UI.DropDownListEventArgs e)
{
string
selectedproductId=
//query for getting the product id from the marketmapping table
foreach
(GridDataItem ditem
in
radProductsGrid.MasterTableView.Items)
{
string
Productid= ditem.OwnerTableView.DataKeyValues[ditem.ItemIndex][
"ProductID"
].ToString();
if
(selectedproductId == Productid)
{
CheckBox check = (CheckBox)ditem.FindControl(
"ProductInMarket"
);
check.Checked =
true
;
break
;
}
}
}
Thanks,
Princy.