简体   繁体   English

在WPF C#中动态创建的网格的最后两行重叠

[英]Overlapping last two rows of dynamically created Grid in wpf C#

I have created dynamically a Grid control with two columns and populate its Rows using data from the database in first column and a textbox control in the second column. 我动态创建了一个具有两列的Grid控件,并使用第一列中的数据库数据和第二列中的文本框控件填充了其Rows。 But the last two rows of grid gets overlapped. 但是最后两行网格重叠。 The following is my code snippet. 以下是我的代码段。

    if (comboBox1.SelectedIndex > 0)
    {
        // Create the Grid
        Grid DynamicGrid = new Grid();
        DynamicGrid.Width = 400;
        DynamicGrid.HorizontalAlignment = HorizontalAlignment.Left;
        DynamicGrid.VerticalAlignment = VerticalAlignment.Top;
        DynamicGrid.ShowGridLines = true;
        DynamicGrid.Background = new SolidColorBrush(Colors.LightSteelBlue);

        //Adding columns to datagrid
        ColumnDefinition gridCol1 = new ColumnDefinition();
        ColumnDefinition gridCol2 = new ColumnDefinition();
        DynamicGrid.ColumnDefinitions.Add(gridCol1);
        DynamicGrid.ColumnDefinitions.Add(gridCol2);

        // Add first column header
        TextBlock txtBlock1 = new TextBlock();
        txtBlock1.Text = "Particulars";
        txtBlock1.FontSize = 14;
        txtBlock1.FontWeight = FontWeights.Bold;
        txtBlock1.Foreground = new SolidColorBrush(Colors.Green);
        txtBlock1.VerticalAlignment = VerticalAlignment.Top;
        Grid.SetRow(txtBlock1, 0);
        Grid.SetColumn(txtBlock1, 0);

        // Add second column header
        TextBlock txtBlock2 = new TextBlock();
        txtBlock2.Text = "Amount";
        txtBlock2.FontSize = 14;
        txtBlock2.FontWeight = FontWeights.Bold;
        txtBlock2.Foreground = new SolidColorBrush(Colors.Green);
        txtBlock2.VerticalAlignment = VerticalAlignment.Top;
        Grid.SetRow(txtBlock2, 0);
        Grid.SetColumn(txtBlock2, 1);

        //// Add column headers to the Grid
        DynamicGrid.Children.Add(txtBlock1);
        DynamicGrid.Children.Add(txtBlock2);

        RowDefinition gridRow;
        TextBlock par;
        TextBox amt;

        int i = 1;
        string pjt = comboBox1.SelectedItem.ToString();
        //Display all the Particulars
        SqlCeConnection con;
        con = dbConnection();
        SqlCeCommand cmd;
        string sql = "select * from " + pjt;
        try
        {
            cmd = new SqlCeCommand(sql, con);
            SqlCeDataReader rdr = cmd.ExecuteReader();
            int ordName = rdr.GetOrdinal("Name");

            while (rdr.Read())
            {
                string nam = rdr.GetString(ordName);
                gridRow = new RowDefinition();
                gridRow.Height = new GridLength(45);
                DynamicGrid.RowDefinitions.Add(gridRow);

                par = new TextBlock();
                par.Text = nam;
                par.FontSize = 12;
                par.FontWeight = FontWeights.Bold;
                Grid.SetRow(par, i);
                Grid.SetColumn(par, 0);

                amt = new TextBox();
                amt.Height = 20;
                amt.Width = 190;
                amt.Foreground = new SolidColorBrush(Colors.Black);
                Grid.SetRow(amt, i);
                Grid.SetColumn(amt, 1);

                DynamicGrid.Children.Add(par);
                DynamicGrid.Children.Add(amt);
                i++;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex);
        }
        this.Content = DynamicGrid;
    }

I can't figure out why this overlapping occurs. 我不知道为什么会发生这种重叠。 Please help me out. 请帮帮我。

I got the solution. 我找到了解决方案。 I just added code to clear the children, rowDefinition and columndefinition of grid before initializing with values. 我只是添加了代码,以在使用值初始化之前清除网格的子级,rowDefinition和columndefinition。

Here i the code I used. 这是我使用的代码。

grid1.Children.Clear();
grid1.RowDefinitions.Clear();
grid1.ColumnDefinitions.Clear();

//Adding columns to datagrid
ColumnDefinition gridCol1 = new ColumnDefinition();
ColumnDefinition gridCol2 = new ColumnDefinition();
grid1.ColumnDefinitions.Add(gridCol1);
grid1.ColumnDefinitions.Add(gridCol2);

// Add first column header
TextBlock txtBlock1 = new TextBlock();
txtBlock1.Text = "Particulars";
txtBlock1.FontSize = 14;
txtBlock1.FontWeight = FontWeights.Bold;
txtBlock1.Foreground = new SolidColorBrush(Colors.Green);
txtBlock1.VerticalAlignment = VerticalAlignment.Top;
Grid.SetRow(txtBlock1, 0);
Grid.SetColumn(txtBlock1, 0);

// Add second column header
TextBlock txtBlock2 = new TextBlock();
txtBlock2.Text = "Amount";
txtBlock2.FontSize = 14;
txtBlock2.FontWeight = FontWeights.Bold;
txtBlock2.Foreground = new SolidColorBrush(Colors.Green);
txtBlock2.VerticalAlignment = VerticalAlignment.Top;
Grid.SetRow(txtBlock2, 0);
Grid.SetColumn(txtBlock2, 1);

//// Add column headers to the Grid
grid1.Children.Add(txtBlock1);
grid1.Children.Add(txtBlock2);

RowDefinition gridRow = new RowDefinition();
grid1.RowDefinitions.Add(gridRow);
TextBlock par;
TextBox amt;

int i = 1;
string pjt = comboBox1.SelectedItem.ToString();


//Display all the Particulars
SqlCeConnection con;
con = dbConnection();
SqlCeCommand cmd;
string sql = "select * from " + pjt;
try
{
    cmd = new SqlCeCommand(sql, con);
    SqlCeDataReader rdr = cmd.ExecuteReader();
    int ordName = rdr.GetOrdinal("Name");

    while (rdr.Read())
    {
        string nam = rdr.GetString(ordName);
        gridRow = new RowDefinition();
        gridRow.Height = new GridLength(45);
        grid1.RowDefinitions.Add(gridRow);

        par = new TextBlock();
        par.Text = nam;
        par.FontSize = 12;
        par.FontWeight = FontWeights.Bold;
        Grid.SetRow(par, i);
        Grid.SetColumn(par, 0);

        amt = new TextBox();
        amt.Height = 20;
        amt.Width = 190;
        amt.Foreground = new SolidColorBrush(Colors.Black);
        Grid.SetRow(amt, i);
        Grid.SetColumn(amt, 1);

        grid1.Children.Add(par);
        grid1.Children.Add(amt);
        i++;
    }                   
}
catch (Exception ex)
{
    MessageBox.Show("Error: " + ex);
}

I placed a grid in the XAML file itself instead of creating dynamically. 我在XAML文件本身中放置了一个网格,而不是动态创建。 Then added rows and columns dynamically. 然后动态添加行和列。 Thanks for the help. 谢谢您的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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