如何显示在Datagrid中选择的项目与众不同?

时间:2011-07-28 17:25:48

标签: c# asp.net sql vb.net

我正在使用Datagrid控件来填充项目/内容列表。我想要显示哪个行被选中,与众不同。

我应该使用datagrid控件的哪些属性?

我该怎么做?

由于 罗恩。

3 个答案:

答案 0 :(得分:0)

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 + ".";

  }

答案 1 :(得分:0)

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);
      }
    }
  }
}

答案 2 :(得分:0)

<SelectedItemStyle BackColor="Pink" ForeColor="Green" />