0

ドロップダウンリストに基づいてグリッドビューを表示する必要があります:LastestTransactionFirst、EarlierTransactionFirst。つまり、基本的には、gridviewの日付に基づいたdescまたはascです。どうすればいいですか?

これは、グリッドビューを選択するための私のコードです。しかし、ドロップダウンリストがあり、ドロップダウンリストの選択に基づいてグリッドビューを表示する必要があります。

    myConnection.ConnectionString = strConnectionString;
    SqlCommand cmd = new SqlCommand("SELECT thDate, thType, thAmountIn, thAmountOut from [Transaction] ORDER BY thDate, thType, thAmountIn, thAmountOut DESC", myConnection);
    myConnection.Open();
    SqlDataReader reader1 = cmd.ExecuteReader();
    GridView1.DataSource = reader1;
    GridView1.DataBind();
4

2 に答える 2

1

1)クエリで動的順序付けを使用できます。できればストアドプロシージャを使用してください。

2)手っ取り早い方法は、ドロップダウンリストでselectedValueを渡し、選択されたインデックス変更イベントとグリッドビューを再バインドし、ドロップダウンリストで自動ポストバックをtrueに有効にすることです。何かのようなもの

protected void yourDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
   BindYourGridView(yourDropDown.SelectedValue);  
}

BindYourGridView(string sortParam)
{
   string orderBy=null;
   switch sortParam
   {
         case 0:
             orderBy= "ORDER BY thDate, thType, thAmountIn, thAmountOut DESC"
             break;
         case 1:
             orderBy= "ORDER BY thDate, thType, thAmountIn, thAmountOut"
             break;   
   }
  string yourQuery= "Select columns from table "+ orderBy;
  // Your data access code
  // Bind your gridview
}


//ASPX
   <asp:DropDownList ID="yourDropDownList" runat="server" AutoPostBack="True">
   <asp:ListItem Text="Recent First" Value="0" />
   <asp:ListItem Text="Earlier First" Value="1" />
   </asp:DropDownList>
于 2012-07-27T06:06:46.263 に答える
0

This is an example of sorting in gridview

DataView dvItems = new DataView((DataTable)ds.Tables["datatable1"]);

 if (ddl_itemsorderby.SelectedValue == "MenuGroup")
     dvItems.Sort = "Menu_Group, Item_Name ASC";
 else if (ddl_itemsorderby.SelectedValue == "Item")
     dvItems.Sort = "Item_Name, Menu_Group ASC";
 else if (ddl_itemsorderby.SelectedValue == "Rate")
     dvItems.Sort = "Item_rate, Item_Name ASC";
 else if (ddl_itemsorderby.SelectedValue == "Quantity")
     dvItems.Sort = "Item_Quantity, Item_Name ASC";

 gridview1.DataSource = dvItems;
 gridview1.DataBind();

Here Menu_Group,Item_name ... are the column names in the datatable which you bind to the gridview.

"Menu_Group, Item_Name ASC" This means that The forse order prority will be given to the Menu_Group first and second order priority will be given to Item_Name. ASC is the Order type will be ascending

ddl_itemsorderby is the dropdownlist from which the column order will be selected.
dvitems is the dataview.

ASC is the ascending order.

You have to create a dataview containing the data in the datatable and then sort the values in the dataview and then bind the dataview to the gridview

于 2012-07-27T06:22:14.277 に答える