简体   繁体   中英

C# - How can I set my Datagrid enabled = false and still have a scrollbar without using readonly()?

I was requested to make an app that read barcodes and update a datatable on the screen that would update the database after the data gathering.

The problem is that there's a column that must be updated at execution time every time a barcode would be read, but the user shouldn't be able to edit the cell by writing on the device keyboard.

So I tried to set my columns to readonly, but when I do this, it's not possible to update the value on the datatable, then I tried to set the DataGrid enabled = false, but then the scrollbar stopped working, and I need it because it's a small device and there are other columns that the user should be able to see.

Is there a simple way to solve this ? I'm using .net compact 3.5

This is my code for button that loads the DataGrid:

DataTable tabela    = new DataTable();
DataTable codbarras = new DataTable();

private void button1_Click(object sender, EventArgs e)
{
    ultimopedido = textBox1.Text;
    try
    {
        FbDataReader leitor;
        FbDataReader leitorbarras;
        string query1 = "SELECT CAST(0 AS DOUBLE PRECISION)SEPARADO, CAST(ITEMORCAMENTO.SALDOITEMORCA - ITEMORCAMENTO.QUANTITEMSEPARADO AS DOUBLE PRECISION)SALDO, ITEMORCAMENTO.QUANTITEMSEPARADO, ITEMORCAMENTO.SALDOITEMORCA, PRODUTO.PRODUTOID, PRODUTO.REFPROD, PRODUTO.NOMEPROD, PRODUTO.ESTOQUEPED, ITEMORCAMENTO.QUANTITEMORCA, ITEMORCAMENTO.ITEMORCAMENTOID, ORCAMENTO.STATUSORCA";
        query1 += " FROM ORCAMENTO ";
        query1 += " LEFT JOIN ITEMORCAMENTO ";
        query1 += " ON ORCAMENTO.ORCAMENTOID = ITEMORCAMENTO.ORCAMENTOID ";
        query1 += " INNER JOIN PRODUTO ";
        query1 += " ON ITEMORCAMENTO.PRODUTOID = PRODUTO.PRODUTOID ";
        query1 += " WHERE ORCAMENTO.ORCAMENTOID = " + textBox1.Text + " AND ";
        query1 += " ORCAMENTO.STATUSORCA = 4 AND ";
        query1 += " ORCAMENTO.SEPARADO = 0 AND ";
        query1 += " ITEMORCAMENTO.STATUSITEMORCA IN (0,1) ";
        query1 += " ORDER BY PRODUTO.PRODUTOID";
        string queryUp = "UPDATE ORCAMENTO SET SEPARADO = 1 WHERE ORCAMENTOID =" + textBox1.Text;


        leitor = conexao.Executar(Conexao.FbConexao, query1);
        tabela.Load(leitor);
        if (tabela.Rows.Count > 0)
        {
            conexao.Atualizar(Conexao.FbConexao, queryUp);
            dataGrid1.DataSource = tabela;
            DataGridTableStyle ts               = new DataGridTableStyle();
            DataGridColumnStyle celId           = new DataGridTextBoxColumn();
            DataGridColumnStyle celQuantitem    = new DataGridTextBoxColumn();
            DataGridColumnStyle celSaldo        = new DataGridTextBoxColumn();
            DataGridColumnStyle celRef          = new DataGridTextBoxColumn();
            ts.MappingName = tabela.TableName;
            celId.MappingName           = "PRODUTOID";
            celId.HeaderText            = "Id";
            celQuantitem.MappingName    = "SEPARADO";
            celQuantitem.HeaderText     = "Separado";
            celSaldo.MappingName        = "SALDO";
            celSaldo.HeaderText         = "Saldo";
            celRef.MappingName          = "REFPROD";
            celRef.HeaderText           = "Referência";
            celId.Width = 50;
            celQuantitem.Width = 75;
            celSaldo.Width = 50;
            celRef.Width = 75;
            ts.GridColumnStyles.Add(celId);
            ts.GridColumnStyles.Add(celQuantitem);
            ts.GridColumnStyles.Add(celSaldo);
            ts.GridColumnStyles.Add(celRef);
            dataGrid1.TableStyles.Add(ts);

            tabela.Columns["SEPARADO"].ReadOnly = true;

            textBox1.Enabled    = false;
            button1.Enabled     = false;
            textBox2.Enabled    = true;
            button6.Enabled     = true;
            button2.Enabled     = true;
            button3.Enabled     = true;
            button4.Enabled     = true;
            button5.Enabled     = true;
        }
        else
        {
            MessageBox.Show("Não foram encontrados dados disponíveis");
        }

        string query2 = "SELECT B.CODBARRAS, B.PRODUTOID, B.ITEMORCAMENTOID" +
                        " FROM ITEMORCAMENTO A" +
                        " INNER JOIN CODBARRASPRODUTO B" +
                        " ON A.PRODUTOID = B.PRODUTOID" +
                        " WHERE B.ITEMORCAMENTOID = 0 AND" +
                        " A.ORCAMENTOID = " + textBox1.Text + "AND " +
                        " B.ITEMORCAMENTOID = 0";
        leitorbarras = conexao.Executar(Conexao.FbConexao, query2);
        codbarras.Load(leitorbarras);
    }
    catch (Exception E)
    {
        MessageBox.Show(E.Message);
    }
}

I think a readonly collection might work for you. http://msdn.microsoft.com/en-us/library/ms132474(v=vs.110).aspx

But there is another way too, you can change the cell type to be a label or textblock. To do that you will need to change the datagrid templates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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