简体   繁体   English

如何在AspxGridView中安排FieldName?

[英]How to arrange the FieldName in AspxGridView?

I want to arrange the Field Names as user wants to display. 我想按用户要显示的方式排列字段名称。 I am not using SqlDataSource. 我没有使用SqlDataSource。 I am calling the stored procedure by programming like below. 我通过如下编程来调用存储过程。

string cs = ConfigurationManager.ConnectionStrings["HQMatajerConnectionString"].ConnectionString;

using (SqlConnection con = new SqlConnection(cs))
{
   SqlCommand cmd = new SqlCommand("spGetTotalSalesQuantity",con);
   cmd.CommandType = System.Data.CommandType.StoredProcedure;
   cmd.Parameters.AddWithValue("@DateFrom", DateFromStr);
   cmd.Parameters.AddWithValue("@DateTo", DateToStr);
   //cmd.Parameters.AddWithValue("@DateFrom", "2015-01-01 00:00:00");
   //cmd.Parameters.AddWithValue("@DateTo", "2015-12-31 23:59:59");

   con.Open();

   SqlDataAdapter sda = new SqlDataAdapter(cmd);
   sda.Fill(ds);

   ASPxGridView1.DataSource = ds;
   ASPxGridView1.DataBind();
}

In result, I can see the field name how i have given the Column_name in my query. 结果,我可以在查询中看到字段名称的输入方式。 But, User wants to see the Field Name how they are arranging. 但是,用户希望查看字段名称的排列方式。

For Example: 例如:

select 
    student_id,student_name ,Class,School_Name
From
    Student_Details

If above one is my stored procedure. 如果以上是我的存储过程。 I will get the Field Name how I mentioned in my query. 我将获得查询中提到的字段名称。

But User wants to see the result how they are giving. 但是用户希望看到结果如何。 If user give School_Name,Class,Student_id,School_Name . 如果用户提供School_Name,Class,Student_id,School_Name

Is there anyway to arrange in AspxGridView? 反正在AspxGridView中有安排吗?

Check my answer Based on my understanding of the question and @mohamed's comment. 根据我对问题的理解和@mohamed的评论,检查我的答案。

Try to use the DataColumn.SetOrdinal method. 尝试使用DataColumn.SetOrdinal方法。 For example: 例如:

con.Open();

   SqlDataAdapter sda = new SqlDataAdapter(cmd);
   sda.Fill(ds);

   ds.Tables[0].Columns["School_Name"].SetOrdinal(0);
   ds.Tables[0].Columns["Class"].SetOrdinal(1); 
   ds.Tables[0].Columns["Student_id"].SetOrdinal(2);
   ds.Tables[0].Columns["School_Name"].SetOrdinal(3); 

   ASPxGridView1.DataSource = ds;
   ASPxGridView1.DataBind();

even if user changes the select statement order of columns will not change. 即使用户更改了select语句的列顺序也不会改变。

  1. Use the hiddentext field and get the column names as user wants to display in which order. 使用hiddentext字段并获得列名称,因为用户希望以该顺序显示。

    string selectedColumns = HiddentxtSelectedColumn.Value;

  2. Move the selectedColumns to array selectedColumns移动到数组

     string[] names = selectedColumns.Split(','); sda.Fill(ds); for (int i = 0; i < names.Length; i++) { ds.Tables[0].Columns[names[i]].SetOrdinal(i);` } ASPxGridView1.DataSource = ds; ASPxGridView1.DataBind(); 

Now It will run exactly. 现在它将完全运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM