简体   繁体   中英

How do I get rid of the empty table in my gridview WPF?

So I recently created a GridView in WPF for my database application. It holds the usernames and passwords for my website users and it works perfectly, The only issue is that it adds the empty columns to the right but I have no diea why. What did I do wrong and how do properly do it? I cant click the right columns its like its just null.

XML

<DataGrid HeadersVisibility="Column" CanUserAddRows="False" BorderBrush="Black" Name="dgAccounts" Foreground="Black" Margin="10,103,405,10">
                <DataGrid.Columns>
                </DataGrid.Columns>
            </DataGrid>

C#

private void RetrieveUserData()
        {
            string line;
            using (WebClient wc = new WebClient())
            {
                string accs = wc.DownloadString(Clipboard.GetText());

                using (StringReader sr = new StringReader(accs))
                {
                    while ((line = sr.ReadLine()) != null)
                    {
                        newAccounts.Add(new Accounts { Username = theUsername, Password = thePassword });
                    }
                }
            }
            dgAccounts.ItemsSource = newAccounts;
        }

Here you can see what the problem looks like

Assuming your DataGrid is a fixed size, the empty column will be generated as a result of your other columns only using the space they need.

Try setting the Width of each column, I suggest using Width="*" to ensure the whole grid is used and each column uses an equal amount of space.

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