DataGridColumn.SortExpression 속성
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
정렬할 열을 선택한 경우 OnSortCommand(DataGridSortCommandEventArgs) 메서드에 전달할 필드 또는 식 이름을 가져오거나 설정합니다.
public:
virtual property System::String ^ SortExpression { System::String ^ get(); void set(System::String ^ value); };
public virtual string SortExpression { get; set; }
member this.SortExpression : string with get, set
Public Overridable Property SortExpression As String
속성 값
정렬할 열을 선택하는 경우 OnSortCommand(DataGridSortCommandEventArgs)에 전달할 필드의 이름입니다. 기본값은 Empty입니다.
예제
다음 코드 예제를 사용 하는 방법에 설명 합니다 SortExpression 정렬에 사용할 필드 이름을 지정 하는 속성입니다.
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<script runat="server">
ICollection CreateDataSource()
{
// Create a Random object to mix up the order of items in the
// sample data.
Random Rand_Num = new Random();
// Create sample data for the DataGrid control.
DataTable dt = new DataTable();
DataRow dr;
// Define the columns of the table.
dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(String)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(Double)));
// Populate the table with sample values.
for (int i=0; i<=8; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Item " + Rand_Num.Next(1, 15).ToString();
dr[2] = 1.23 * Rand_Num.Next(1, 15);
dt.Rows.Add(dr);
}
// To persist the data source between posts to the server,
// store it in session state.
Session["Source"] = dt;
DataView dv = new DataView(dt);
return dv;
}
void Page_Load(Object sender, EventArgs e)
{
// Load sample data only once, when the page is first loaded.
if (!IsPostBack)
{
ItemsGrid.DataSource = CreateDataSource();
ItemsGrid.DataBind();
}
}
void Sort_Grid(Object sender, DataGridSortCommandEventArgs e)
{
// Retrieve the data source from session state.
DataTable dt = (DataTable)Session["Source"];
// Create a DataView from the DataTable.
DataView dv = new DataView(dt);
// The DataView provides an easy way to sort. Simply set the
// Sort property with the name of the field to sort by.
dv.Sort = e.SortExpression;
// Re-bind the data source and specify that it should be sorted
// by the field specified in the SortExpression property.
ItemsGrid.DataSource = dv;
ItemsGrid.DataBind();
}
</script>
<head runat="server">
<title>DataGrid SortExpression Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>DataGrid SortExpression Example</h3>
<b>Product List</b>
<asp:DataGrid id="ItemsGrid"
BorderColor="black"
BorderWidth="1"
CellPadding="3"
OnSortCommand="Sort_Grid"
AutoGenerateColumns="False"
AllowSorting="True"
runat="server">
<HeaderStyle BackColor="#00aaaa">
</HeaderStyle>
<Columns>
<asp:BoundColumn DataField="IntegerValue"
SortExpression="IntegerValue"
HeaderText="Item"/>
<asp:BoundColumn DataField="StringValue"
SortExpression="StringValue"
HeaderText="Description"/>
<asp:BoundColumn DataField="CurrencyValue"
HeaderText="Price"
SortExpression="CurrencyValue"
DataFormatString="{0:c}">
<ItemStyle HorizontalAlign="Right">
</ItemStyle>
</asp:BoundColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<script runat="server">
Function CreateDataSource() As ICollection
' Create a Random object to mix up the order of items in the
' sample data.
Dim Rand_Num As Random = New Random()
' Create sample data for the DataGrid control.
Dim dt As DataTable = New DataTable()
Dim dr As DataRow
' Define the columns of the table.
dt.Columns.Add(new DataColumn("IntegerValue", GetType(Int32)))
dt.Columns.Add(new DataColumn("StringValue", GetType(String)))
dt.Columns.Add(new DataColumn("CurrencyValue", GetType(Double)))
' Populate the table with sample values.
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " & Rand_Num.Next(1, 15).ToString()
dr(2) = 1.23 * Rand_Num.Next(1, 15)
dt.Rows.Add(dr)
Next i
' To persist the data source between posts to the server,
' store it in session state.
Session("Source") = dt
Dim dv As DataView = New DataView(dt)
Return dv
End Function
Sub Page_Load(sender As Object, e As EventArgs)
' Load sample data only once, when the page is first loaded.
If Not IsPostBack Then
ItemsGrid.DataSource = CreateDataSource()
ItemsGrid.DataBind()
End If
End Sub
Sub Sort_Grid(sender As Object, e As DataGridSortCommandEventArgs)
' Retrieve the data source from session state.
Dim dt As DataTable = CType(Session("Source"), DataTable)
' Create a DataView from the DataTable.
Dim dv As DataView = New DataView(dt)
' The DataView provides an easy way to sort. Simply set the
' Sort property with the name of the field to sort by.
dv.Sort = e.SortExpression
' Re-bind the data source and specify that it should be sorted
' by the field specified in the SortExpression property.
ItemsGrid.DataSource = dv
ItemsGrid.DataBind()
End Sub
</script>
<head runat="server">
<title>DataGrid SortExpression Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>DataGrid SortExpression Example</h3>
<b>Product List</b>
<asp:DataGrid id="ItemsGrid"
BorderColor="black"
BorderWidth="1"
CellPadding="3"
OnSortCommand="Sort_Grid"
AutoGenerateColumns="False"
AllowSorting="True"
runat="server">
<HeaderStyle BackColor="#00aaaa">
</HeaderStyle>
<Columns>
<asp:BoundColumn DataField="IntegerValue"
SortExpression="IntegerValue"
HeaderText="Item"/>
<asp:BoundColumn DataField="StringValue"
SortExpression="StringValue"
HeaderText="Description"/>
<asp:BoundColumn DataField="CurrencyValue"
HeaderText="Price"
SortExpression="CurrencyValue"
DataFormatString="{0:c}">
<ItemStyle HorizontalAlign="Right">
</ItemStyle>
</asp:BoundColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
설명
사용 하 여는 SortExpression 속성에 전달할 필드의 이름을 지정 하거나 확인을 OnSortCommand 메서드 정렬에 대 한 열을 선택 합니다.
참고
이 속성의 필드 데이터 원본에 열을 바인딩하지 않습니다. 열 필드에 바인딩할 특정 열 형식에 대 한 설명서를 참조 하십시오.
정렬을 사용 하는 경우 LinkButton 컨트롤은 각 열의 머리글 구역에 렌더링 됩니다 여기서는 SortExpression 속성을 설정 합니다.
참고
경우는 AutoGenerateColumns 속성이로 설정 되어 true
, SortExpression 의 각 열에 대 한 속성을 DataGrid 컨트롤 데이터 바인딩을 지 원하는 열에 바인딩된 필드 이름을 사용 하 여 자동으로 설정 됩니다. 경우는 AutoGenerateColumns 속성이로 설정 되어 false
를 수동으로 설정 해야 합니다 SortExpression 필드 이름 사용 하 여 속성에 대 한 정렬을 사용 하도록 설정 하려는 각 열에 대 한 열에 바인딩된.
이러한 LinkButton 컨트롤을 사용 하면 정렬 하는 DataGrid 선택한 열을 기준으로 제어 합니다. 경우는 LinkButton 열을 클릭 하면에 대 한 제어를 SortCommand 이벤트가 발생 합니다. 값을 SortExpression 선택한 열에 대 한 속성에 전달 되는 OnSortCommand 이벤트 처리기의 일부로 DataGridSortCommandEventArgs 개체.
적용 대상
추가 정보
.NET
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기