Readonly datagrid columns with sql

Ahmed :

i have a datagrid with mysql data and checkbox columns

here is the code

    MySqlCommand cmd = m.getmodel("select * from " + equip + " where stored=" + type);
    MySqlDataAdapter dataAdapter = new MySqlDataAdapter(cmd);
    DataTable data = new DataTable(equip);
    dataAdapter.Fill(data);
    var checkBoxColumn = new DataColumn("Select", typeof(bool));
    data.Columns.Add(checkBoxColumn);

    checkBoxColumn.SetOrdinal(0); // Move column to the beginning
    foreach (DataRow row in data.Rows)
    {
        row["Select"] = false;

    }

    data2.ItemsSource = data.DefaultView;
    checkBoxColumn.ReadOnly = false;

XAML

   <DataGrid Margin="736,166,10,130"
                  Grid.Column="4"
                  Grid.Row="4"
                  IsReadOnly="true"
                  CanUserAddRows="false"
                  x:Name="data2"
                  SelectionChanged="DataGrid_SelectionChanged" />

now i need ONLY the first column is editable , to allow user use the check box , see the pic

d

i need to restrict editing on the rest of data EXCEPT the checkbox column i have tried Readonly in xaml but it's restricting the editing even in first column i don't know how to add rows manually while iam using mysqlcommand

Tấn Nguyên :

I would start in before your code line data2.ItemsSource = data.DefaultView;. Remember that you already removed IsReadOnly="true" in your DataGrid XAML

Simple readonly setting

        foreach (DataColumn col in data.Columns)
        {
            if (col.ColumnName == "Select")
                col.ReadOnly = false;
            else
                col.ReadOnly = true;
        }

        dataGrid.ItemsSource = data.DefaultView;

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=405314&siteId=1