简体   繁体   English

为什么在添加到datagridview的链接时只添加一行?

[英]Why when adding links to datagridview it's adding only one row?

In this case there are 40 links. 在这种情况下,有40个链接。 but when running the program i see it's adding only one row the last one. 但是运行该程序时,我看到它仅在最后一行中添加一行。

public Form1()
        {
            InitializeComponent();

            label2.Visible = false;
            label3.Visible = false;
            label4.Visible = false;
            label7.Visible = false;

            tbxMainDownloadPath.Text = Properties.Settings.Default.LastSelectedFolder;
            if (tbxMainDownloadPath.Text != "")
                downloadDirectory = tbxMainDownloadPath.Text;

            tracker = new DownloadProgressTracker.DownloadProgress(50, TimeSpan.FromMilliseconds(500));
            string[] countries = File.ReadAllLines(@"CountriesNames.txt");
            string[] countriesCodes = File.ReadAllLines(@"CountriesCodes.txt");
            foreach (string country in countries)
            {
                countryList.Add(country);
                string countryPath = Path.Combine(downloadDirectory, country);
                if (!Directory.Exists(countryPath))
                    Directory.CreateDirectory(countryPath);
            }
            foreach (string code in countriesCodes)
            {
                codesList.Add(code);
            }
            codeToFullNameMap = codesList
                .Select((code, index) => index)
                .ToDictionary(
                              keySelector: index => codesList[index],
                              elementSelector: index => countryList[index]);

            lines = File.ReadAllLines(@"links.txt");
            for (int i = 0; i < lines.Length; i++)
            {
                RichTextBoxExtensions.AppendText(true, richTextBox1, "Ready: ", Color.Red, 8.25f);
                richTextBox1.AppendText(lines[i] + (i < lines.Length - 1 ? Environment.NewLine : String.Empty));
            }

            for (int i = 0; i < countriesCodes.Length; i++)
            {
                dataGridView1.Columns.Clear();
                dataGridView1.ColumnCount = 2;
                dataGridView1.Columns[0].Name = "Country";
                dataGridView1.Columns[1].Name = "Code";
                var countryName = codeToFullNameMap[countriesCodes[i]];
                string[] row = new string[] { countryName, countriesCodes[i] };
                dataGridView1.Rows.Add(row);
                DataGridViewLinkColumn dgvLink = new DataGridViewLinkColumn();
                dgvLink.UseColumnTextForLinkValue = true;
                dgvLink.LinkBehavior = LinkBehavior.SystemDefault;
                dgvLink.HeaderText = "Link Data";
                dgvLink.Name = "SiteName";
                dgvLink.LinkColor = Color.Blue;
                dgvLink.TrackVisitedState = true;
                dgvLink.Text = lines[i];
                dgvLink.UseColumnTextForLinkValue = true;
                dataGridView1.Columns.Add(dgvLink);
            }
        }

In countriesCodes there are 40 items. 在国家/地区代码中,有40个项目。

数据网格视图

The first problem is that it's adding only one country and it's link and it's the last one, it's not adding the rest. 第一个问题是,它仅添加了一个国家/地区,并且是链接,并且是最后一个国家/地区,没有添加其他国家/地区。

Second problem is how can i make it to be moved to the right the ? 第二个问题是如何使它向右移动? I mean when running i'm using the mouse to drag the link column to the right to the datagridview end side on the right. 我的意思是在运行时,我使用鼠标将链接列拖动到右侧的datagridview端右侧。 How can i make to be already to the right end side ? 我如何才能已经在右端? Screenshot added after i dragged it to the right: 我将其拖动到右侧后添加的屏幕截图:

右侧的链接数据列

And third last problem how to remove the highlight from Country ? 最后一个问题是如何从“国家/地区”中删除重点? I don't want it to highlight anything. 我不希望它突出显示任何内容。 Now the highlight is automatic on the country when running the program. 现在,在运行该程序时,重点自动显示在国家/地区上。

Upodate 更新

Solved all the problems i had: 解决了我遇到的所有问题:

for (int i = 0; i < countriesCodes.Length; i++)
            {
                dataGridView1.ColumnCount = 2;
                dataGridView1.Columns[0].Name = "Country";
                dataGridView1.Columns[1].Name = "Code";
                var countryName = codeToFullNameMap[countriesCodes[i]];
                string[] row = new string[] { countryName, countriesCodes[i] };
                dataGridView1.Rows.Add(row);
                DataGridViewLinkColumn dgvLink = new DataGridViewLinkColumn();
                dgvLink.UseColumnTextForLinkValue = true;
                dgvLink.LinkBehavior = LinkBehavior.SystemDefault;
                dgvLink.HeaderText = "Link Data";
                dgvLink.Name = "SiteName";
                dgvLink.LinkColor = Color.Blue;
                dgvLink.TrackVisitedState = true;
                dgvLink.Text = lines[i];
                dgvLink.UseColumnTextForLinkValue = true;
                dataGridView1.Columns.Add(dgvLink);
            }
            this.dataGridView1.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView1.EnableHeadersVisualStyles = false;
            dataGridView1.DefaultCellStyle.SelectionBackColor = dataGridView1.DefaultCellStyle.BackColor;
            dataGridView1.DefaultCellStyle.SelectionForeColor = dataGridView1.DefaultCellStyle.ForeColor;

            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.BackgroundColor = System.Drawing.SystemColors.Control;
        for (int i = 0; i < countriesCodes.Length; i++)
        {
            dataGridView1.Columns.Clear();

You are clearing your columns 40 times, so only the last iteration through the loop leaves anything to display. 您将清除列40次,因此只有循环中的最后一次迭代才能显示任何内容。 Do all of your dataGridView1.Columns setup before the for loop. 在for循环之前完成所有的dataGridView1.Columns设置。 The for loop should only be adding Rows. for循环应仅添加行。

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

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