1

I have two tables within a DataSet that I relate as parent-child. I'd like to display and edit them within the same DataGridView. So I added the parent column of interest to the child table.

Visualization is ok. In DataGridView I see all child rows, and those from the parent table are listed multiple times (which is intended - other topic). But when it comes to editing (double-click on cell or press "F2") it is not possible, i.e. I can only edit columns from the original child table. Trying to force the column to ReadOnly = false; will result in an error.

The reason to chose this form of display is to get the data in a compact way to my application. There will be some additional logic for adding and removing lines from the data table, which is beyond the scope of this problem.

Attached a small demo program:

class DataGridViewDemo : Form
{
    private DataGridView dataGridView1 = new DataGridView();

    [STAThreadAttribute()]
    public static void Main()
    {
        Application.Run(new DataGridViewDemo());
    }


    public DataGridViewDemo()
    {
        this.dataGridView1.Dock = DockStyle.Fill;
        this.dataGridView1.AllowUserToAddRows = false;

        this.Controls.Add(this.dataGridView1);
        this.Load += new EventHandler(DataTableProblem);
        this.Text = "DataGridView row demo";
    }


    public void DataTableProblem(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();

        DataTable parentTable = new DataTable("Parent");
        DataTable childTable = new DataTable("Child");

        ds.Tables.Add(parentTable);
        ds.Tables.Add(childTable);

        DataColumn column;
        DataRow row;

        column = new DataColumn("ID", System.Type.GetType("System.Int32"));
        ds.Tables["Parent"].Columns.Add(column);
        column = new DataColumn("ParentName", System.Type.GetType("System.String"));
        ds.Tables["Parent"].Columns.Add(column);

        row = ds.Tables["Parent"].NewRow();
        row["ID"] = 1;
        row["ParentName"] = "Test";
        ds.Tables["Parent"].Rows.Add(row);

        column = new DataColumn("ID", System.Type.GetType("System.Int32"));
        ds.Tables["Child"].Columns.Add(column);
        column = new DataColumn("parentID", System.Type.GetType("System.Int32"));
        ds.Tables["Child"].Columns.Add(column);
        column = new DataColumn("ChildName", System.Type.GetType("System.String"));
        ds.Tables["Child"].Columns.Add(column);

        row = ds.Tables["Child"].NewRow();
        row["ID"] = 1;
        row["parentID"] = 1;
        row["ChildName"] = "My Name";
        ds.Tables["Child"].Rows.Add(row);

        row = ds.Tables["Child"].NewRow();
        row["ID"] = 2;
        row["parentID"] = 1;
        row["ChildName"] = "My other Name";
        ds.Tables["Child"].Rows.Add(row);

        DataRelation relation = new DataRelation(
            "p2c",
            ds.Tables["Parent"].Columns["ID"],
            ds.Tables["Child"].Columns["parentID"]);

        ds.Relations.Add(relation);

        ds.Tables["Child"].Columns.Add(
            "ParentName",
            ds.Tables["Parent"].Columns["ParentName"].DataType,
            "Parent(p2c).ParentName");

        dataGridView1.DataSource = ds.Tables["Child"];
        //dataGridView1.Columns["ParentName"].ReadOnly = false; // --> removing comment will result in an error
        dataGridView1.Refresh();
    }
}

Can anybody hint me in the right direction how to make all columns editable?

Thanks a lot

wizglins
  • 11
  • 3

0 Answers0