如何显示在数据网格中选择的项目的独特性
本文关键字:网格 选择 数据网 项目 独特性 数据 何显示 显示 | 更新日期: 2023-09-27 18:03:45
我使用Datagrid控件来填充项目/内容列表。我想显示被选中的行,有区别性。
我应该使用数据网格控件的什么属性?
我该怎么做呢?
谢谢罗恩。
<SelectedItemStyle BackColor="Pink" ForeColor="Green" />
您可能需要SelectedRow属性
void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
{
// Get the currently selected row using the SelectedRow property.
GridViewRow row = CustomersGridView.SelectedRow;
// Display the company name from the selected row.
// In this example, the third column (index 2) contains
// the company name.
MessageLabel.Text = "You selected " + row.Cells[2].Text + ".";
}
Ron, DataGrid没有默认的行选择行为。你应该自己做:
<asp:DataGrid runat="server" ID="DataGridTest" SelectedIndex="1" OnItemCreated="DataGridTest_ItemCreated" DataSourceID="DataSourceTmp">
<HeaderStyle BackColor="HighlightText" Font-Bold="true" />
<ItemStyle BackColor="White" />
<SelectedItemStyle BackColor="#bbbbff" />
</asp:DataGrid>
背后的代码:
public partial class _Default : Page
{
private const string DataGridSelectedRowCssClass = "selectedRow";
protected void Page_Load(object sender, EventArgs e)
{
Page.ClientScript.RegisterClientScriptBlock(
GetType(),
"dataGrid_selectRow",
string.Format(
@"(function (dataGrid, $, undefined) {{
dataGrid.selectRow = function (row) {{
$(row).siblings('.{0}').css('background-color', '#{1}').end().css('background-color', '#{2}').addClass('{0}');
}}
}})(window.dataGrid = window.dataGrid || {{}}, jQuery);",
DataGridSelectedRowCssClass,
DataGridTest.ItemStyle.BackColor.ToArgb().ToString("X8").Substring(2),
DataGridTest.SelectedItemStyle.BackColor.ToArgb().ToString("X8").Substring(2)),
true);
}
protected void DataGridTest_ItemCreated(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.SelectedItem)
{
e.Item.Attributes["onclick"] = "dataGrid.selectRow(this);";
if (e.Item.ItemType == ListItemType.SelectedItem)
{
e.Item.CssClass = string.Format("{0} {1}", e.Item.CssClass, DataGridSelectedRowCssClass);
}
}
}
}