前台:<asp:BoundColumn HeaderImageUrl="../image/mail_head.bmp" DataField="infor_image" SortExpression="infor_image"
DataFormatString="&lt;img src={0}&gt;">
<HeaderStyle Width="15px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn HeaderImageUrl="../image/important_head.bmp" DataField="urgence_image" SortExpression="urgence_image"
DataFormatString="&lt;img src={0}&gt;">
<HeaderStyle Width="10px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="sendflag" SortExpression="send_flag" HeaderText="S." DataFormatString="&lt;img src={0}&gt;"></asp:BoundColumn>
<asp:BoundColumn DataField="receflag" SortExpression="receive_flag" HeaderText="R." DataFormatString="&lt;img src={0}&gt;"></asp:BoundColumn>
<asp:HyperLinkColumn Target="frrightbottom1" DataNavigateUrlField="information_id" DataNavigateUrlFormatString="rightbottom1.aspx?information_id={0}"
DataTextField="title" SortExpression="title" HeaderText="Title">
<HeaderStyle Width="80px"></HeaderStyle>
</asp:HyperLinkColumn>
<asp:BoundColumn DataField="clerkname" SortExpression="clerkname" HeaderText="Send">
<HeaderStyle Width="45px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="requestinformation" SortExpression="requestinformation" HeaderText="Request">
<HeaderStyle Width="100px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="send_time" SortExpression="send_time" HeaderText="Send Time">
<HeaderStyle Width="110px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="receive_time" SortExpression="receive_time" HeaderText="Receive Time">
<HeaderStyle Width="110px"></HeaderStyle>
</asp:BoundColumn>

解决方案 »

  1.   

    后台:通过session("sort") 实现正排序和倒排序
    vb的,C#的差不多,没有太多要改的,自己看看吧。    Private Sub DataGrid2_SortCommand1(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid2.SortCommand
            If Session("sort") = "0" Or IsNothing(Session("sort")) Then
                show_receive(e.SortExpression)
                Session("sort") = "1"
            Else
                show_receive(e.SortExpression & " DESC")
                Session("sort") = "0"
            End If
        End SubPrivate Sub show_receive(ByVal source As String)
    Dim ds As New DataSet
            Dim dataview1 As DataView
            DAinformation = New SqlDataAdapter(Select_Information, myconnect)
            DAinformation.Fill(ds, "receive")        dataview1 = ds.Tables("receive").DefaultView
            dataview1.Sort = source
            DataGrid2.DataSource = dataview1
            DataGrid2.DataBind()
    End Subselect_information是你的select语句,设定dataview的排序为datagrid上点击的列,然后重新绑定datagrid。
      

  2.   

    ,我层试着将这两个按钮做到DataGrid的模板列的模板里。。又该怎样为该模板列添加事件。
    并且要求在该列事件里面能够索取e.Item.Cells[?].Text取值看快速入门中的
    http://localhost/quickstart/util/srcview.aspx?path=/quickstart/aspplus/samples/webforms/data/datagrid6.src&file=CS\datagrid6.aspx&font=3
      

  3.   

    谢谢热心的朋友不过,可能是我的主题意思没有表达清楚,我想问的主要是,DATAGRID里的按钮事件怎么写,而不是排序怎么实现..
    还有在DATAGRID里面添加模板列中包含的按钮的事件怎么写..
      

  4.   

    按钮事件非常简单的,只要你在模板中加入一个按钮,并在 .cs文件中写一个 protected or public 方法即可.
            1.
               protected void btnClick(object sender, EventArgs e)
     {
    Button btn = (Button)sender;
    if(btn!= null && btn.CommandName== "someCondition")
    {
    ...
    }
    }
          2.
             <ItemTemplate>
              <asp:Button id="Button1" runat="server" Text="Button" onClick= "btnClick"></asp:Button>
             </ItemTemplate>
        3 .ok         
      

  5.   

    btn 中的 CommandName
    也可以先把---->e.Item.Cells[?].Text ,的数据列放入其 CommandName 中,到时直接读取ok.
    方法如下:
            1.假如e.Item.Cells[?].Text 的绑定数据表列名为 xxxxxx;
            2.在btn的CommandName 属性中 
             DataBinder.Eval(Container.DataItem, "xxxxxx").
            3.在btn事件按上面方法获取即可.
            
            这对LinkButton是一样的.
      

  6.   

    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>
    <%@ Import Namespace="System.Globalization" %>
    <%@ Import Namespace="Acme" %><html><script language="C#" runat="server">    SqlConnection myConnection;    protected void Page_Load(Object Src, EventArgs E)
        {
            myConnection = new SqlConnection("server=(local)\\NetSDK;database=pubs;Integrated Security=SSPI");        if (!IsPostBack)
                BindGrid();
        }    public void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs e)
        {
            MyDataGrid.EditItemIndex = (int)e.Item.ItemIndex;
            BindGrid();
        }    public void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs e)
        {
            MyDataGrid.EditItemIndex = -1;
            BindGrid();
        }    public void MyDataGrid_Update(Object sender, DataGridCommandEventArgs e)
        {
            String updateCmd = "UPDATE Authors SET au_id = @Id, au_lname = @LName, au_fname = @FName, phone = @Phone, "
                 + "address = @Address, city = @City, state = @State, zip = @Zip, contract = @Contract where au_id = @Id";        SqlCommand myCommand = new SqlCommand(updateCmd, myConnection);        myCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.NVarChar, 11));
            myCommand.Parameters.Add(new SqlParameter("@LName", SqlDbType.NVarChar, 40));
            myCommand.Parameters.Add(new SqlParameter("@FName", SqlDbType.NVarChar, 20));
            myCommand.Parameters.Add(new SqlParameter("@Phone", SqlDbType.NChar, 12));
            myCommand.Parameters.Add(new SqlParameter("@Address", SqlDbType.NVarChar, 40));
            myCommand.Parameters.Add(new SqlParameter("@City", SqlDbType.NVarChar, 20));
            myCommand.Parameters.Add(new SqlParameter("@State", SqlDbType.NChar, 2));
            myCommand.Parameters.Add(new SqlParameter("@Zip", SqlDbType.NChar, 5));
            myCommand.Parameters.Add(new SqlParameter("@Contract", SqlDbType.NVarChar,1));        myCommand.Parameters["@Id"].Value = MyDataGrid.DataKeys[(int)e.Item.ItemIndex];        String[] cols = {"@Id","@LName","@FName","@Phone","@Address","@City","@State","@Zip","@Contract"};
            Message.InnerHtml = "";        int numCols = e.Item.Cells.Count;
            for (int i=2; i<numCols-1; i++) //skip first, second and last column
            {
                String colvalue =((System.Web.UI.WebControls.TextBox)
                    e.Item.Cells[i].Controls[0]).Text;
                    
                // check for invalid values
                switch (cols[i-1])
                {
                    case "@LName":
                        if ( !InputValidator.IsValidAnsiName(colvalue) )
                        {
                            Message.InnerHtml += "ERROR: Last Name - " + InputValidator.AnsiNameErrorString + "<br>";
                        }
                        break;
                    case "@FName":
                        if ( !InputValidator.IsValidAnsiName(colvalue) )
                        {
                            Message.InnerHtml += "ERROR: First Name - " + InputValidator.AnsiNameErrorString + "<br>";
                        }
                        break;
                    case "@Phone":
                        if ( !InputValidator.IsValidAnsiPhoneNumber(colvalue) )
                        {
                            Message.InnerHtml += "ERROR: Phone - " + InputValidator.AnsiPhoneErrorString + "<br>";
                        }
                        break;
                    case "@Address":
                        if ( !InputValidator.IsValidAnsiAddress(colvalue) )
                        {
                            Message.InnerHtml += "ERROR: Address - " + InputValidator.AnsiAddressErrorString + "<br>";
                        }
                        break;
                    case "@City":
                        if ( !InputValidator.IsValidAnsiCityOrState(colvalue) )
                        {
                            Message.InnerHtml += "ERROR: City - " + InputValidator.AnsiCityStateErrorString + "<br>";
                        }
                        break;
                    case "@State":
                        if ( !InputValidator.IsValidAnsiTwoCharacterState(colvalue) )
                        {
                            Message.InnerHtml += "ERROR: State - " + InputValidator.AnsiTwoCharacterStateErrorString + "<br>";
                        }
                        break;
                    case "@Zip":
                        if ( !InputValidator.IsValidFiveDigitZipCode(colvalue) )
                        {
                            Message.InnerHtml += "ERROR: Zip Code - " + InputValidator.AnsiBasicZipCodeErrorString + "<br>";
                        }
                        break;
                }            // check for null values in required fields
                if (i<6 && colvalue == "")
                {
                    Message.InnerHtml += "ERROR: Null values not allowed for " + cols[i-1] + "<br>";
                }            myCommand.Parameters[cols[i-1]].Value = colvalue;
            }
            
            if ( Message.InnerHtml != "" )
            {
                Message.Style["color"] = "red";
                return;
            }        //append last row, converting true/false values to 0/1
            if (String.Compare(((System.Web.UI.WebControls.TextBox)
                e.Item.Cells[numCols-1].Controls[0]).Text, "true", true, CultureInfo.InvariantCulture)==0)
                myCommand.Parameters["@Contract"].Value =  "1";
            else
                myCommand.Parameters["@Contract"].Value =  "0";        myCommand.Connection.Open();        try
            {
                myCommand.ExecuteNonQuery();
                Message.InnerHtml = "<b>Record Updated</b><br>" + updateCmd;
                MyDataGrid.EditItemIndex = -1;
            }
            catch (SqlException exc)
            {
                if (exc.Number == 2627)
                    Message.InnerHtml = "ERROR: A record already exists with the same primary key";
                else
                    Message.InnerHtml = "ERROR: Could not update record, please ensure the fields are correctly filled out";
                Message.Style["color"] = "red";
            }        myCommand.Connection.Close();        BindGrid();
        }    public void BindGrid()
        {
            SqlDataAdapter myCommand = new SqlDataAdapter("select * from Authors", myConnection);        DataSet ds = new DataSet();
            myCommand.Fill(ds, "Authors");        MyDataGrid.DataSource=ds.Tables["Authors"].DefaultView;
            MyDataGrid.DataBind();
        }</script><body style="font: 10pt verdana">  <form runat="server">    <h3><font face="Verdana">Updating a Row of Data</font></h3>    <span id="Message" EnableViewState="false" style="font: arial 11pt;" runat="server"/><p>    <ASP:DataGrid id="MyDataGrid" runat="server"
          Width="800"
          BackColor="#ccccff"
          BorderColor="black"
          ShowFooter="false"
          CellPadding=3
          CellSpacing="0"
          Font-Name="Verdana"
          Font-Size="8pt"
          HeaderStyle-BackColor="#aaaadd"
          OnEditCommand="MyDataGrid_Edit"
          OnCancelCommand="MyDataGrid_Cancel"
          OnUpdateCommand="MyDataGrid_Update"
          DataKeyField="au_id"
        >      <Columns>
            <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" ItemStyle-Wrap="false"/>
          </Columns>    </ASP:DataGrid>  </form></body>
    </html>