XtraGridView - Where with variable

0

Have a good night, how are you?

I'm creating a user registration screen, where every user has an ID, on this screen, I have an xtragridview, which loads some address information.

However, in this xtragridview table, you have addresses of several other clients, so you would need it to only bring the addresses of that client.

I'm trying to create a query, in the gridview datasource:

select "Cliente_enderecos_alter"."id_cliente_cobranca",
   "Cliente_enderecos_alter"."endereco",
   "Cliente_enderecos_alter"."numero",
   "Cliente_enderecos_alter"."bairro",
   "Cliente_enderecos_alter"."cidade",
   "Cliente_enderecos_alter"."estado",
   "Cliente_enderecos_alter"."cep"
from "dbo"."Cliente_enderecos_alter" "Cliente_enderecos_alter"

where  "Cliente_enderecos_alter"."id_cliente" = @id_cliente

That's where my question comes from, how do I define this parameter @id_cliente ?

If it were a normal query, it would look like this:

 consql._sql = @"SELECT * FROM Login WHERE id_usu = @id_usu";
 SqlCommand cmd = new SqlCommand(consql._sql, sqlconn);
 cmd.Parameters.Add("@id_usu", SqlDbType.Int).Value = id_usu;
 sqlconn.Open();

But in gridview? How do I do it?

Valeuu

    
asked by anonymous 10.07.2016 / 01:44

1 answer

0
Well, guys, good morning. After 1 day and a few hours searching for a solution, I was able to do it as follows:

In Sqldatasource, I create a parameter that I called "queryParameter1", it follows code:

 this.sqlDataSource1.ConnectionName = "String tpspoazsql01 onee";
        this.sqlDataSource1.Name = "sqlDataSource1";
        columnExpression1.ColumnName = "id_cliente_cobranca";
        table1.MetaSerializable = "0|0|125|240";
        table1.Name = "Cliente_enderecos_alter";
        columnExpression1.Table = table1;
        column1.Expression = columnExpression1;
        columnExpression2.ColumnName = "endereco";
        columnExpression2.Table = table1;
        column2.Expression = columnExpression2;
        columnExpression3.ColumnName = "numero";
        columnExpression3.Table = table1;
        column3.Expression = columnExpression3;
        columnExpression4.ColumnName = "bairro";
        columnExpression4.Table = table1;
        column4.Expression = columnExpression4;
        columnExpression5.ColumnName = "cidade";
        columnExpression5.Table = table1;
        column5.Expression = columnExpression5;
        columnExpression6.ColumnName = "estado";
        columnExpression6.Table = table1;
        column6.Expression = columnExpression6;
        columnExpression7.ColumnName = "cep";
        columnExpression7.Table = table1;
        column7.Expression = columnExpression7;
        selectQuery1.Columns.Add(column1);
        selectQuery1.Columns.Add(column2);
        selectQuery1.Columns.Add(column3);
        selectQuery1.Columns.Add(column4);
        selectQuery1.Columns.Add(column5);
        selectQuery1.Columns.Add(column6);
        selectQuery1.Columns.Add(column7);
        selectQuery1.FilterString = "[Cliente_enderecos_alter.id_cliente] = ?id";
        selectQuery1.Name = "Cliente_enderecos_alter";
        queryParameter1.Name = "id";
        queryParameter1.Type = typeof(int);
        queryParameter1.Value = id_cliente;
        selectQuery1.Parameters.Add(queryParameter1);

        selectQuery1.Tables.Add(table1);
        this.sqlDataSource1.Queries.AddRange(new DevExpress.DataAccess.Sql.SqlQuery[] {
        selectQuery1});
        this.sqlDataSource1.ResultSchemaSerializable = resources.GetString("sqlDataSource1.ResultSchemaSerializable");

Well, done that, we have to assign a value to our variable "client_id" before the initialization of the form's components:

 public Manutenção_cliente(int id)
    {
        this.StartPosition = FormStartPosition.CenterScreen;
        id_cliente = id;

        InitializeComponent();

        // This line of code is generated by Data Source Configuration Wizard
        // Fill a SqlDataSource
        sqlDataSource1.Fill();
    }

With this, I was able to perform the query, I do not know if it is the most correct method, if someone can comment.

Thanks

    
10.07.2016 / 17:12