Column order of a datagridview from SQL Server database in WinForms using C#

Viewed 74

I am trying to make my datagridview show data in descending order (from Z to A) based only on one column (name). When the form is loaded, in order to achieve that, I have created a stored procedure with the clause ORDER BY:

CREATE PROC ORDENAR
    @IDCATEGORIA INT,
    @NOMBRE NVARCHAR(30),
    @DESCRIPCION NVARCHAR(256)
AS
    SELECT IDCATEGORIA, NOMBRE 
    FROM clasificacion
    WHERE NOMBRE = @NOMBRE
    ORDER BY NOMBRE DESC;

Then in my C# code, I call that stored procedure:

public void ordenarcategoria(E_CATEGORIA categoria)
{
    using (var conexion = new SqlConnection(ConfigurationManager.ConnectionStrings["conectar"].ConnectionString))
    {
        conexion.Open();

        SqlCommand CMD = new SqlCommand("ORDENAR", conexion);
        CMD.CommandType = CommandType.StoredProcedure;
        CMD.Parameters.AddWithValue("@NOMBRE","");
        CMD.Parameters.AddWithValue("@IDCATEGORIA", 1);
        CMD.Parameters.AddWithValue("DESCRIPCION", "");

        CMD.ExecuteNonQuery();
        conexion.Close();
    }
}

I want my datagridview to show data in descending order with a stored procedure in SQL Server.

I am using WinForms, and here's how my datagridview is loaded:

public void mostrarbuscar(string buscar)
{
    N_clasificacion objn = new N_clasificacion();
    Tablaclasificacion.DataSource = objn.listandoC(buscar);
     
    Tablaclasificacion.ClearSelection();
}

private void fmrClasificacion_Load(object sender, EventArgs e)
{
    mostrarbuscar("");
    accionestabla();
}

tablaclasificacion is the name of my DGV

0 Answers
Related