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

时间:2017-03-12 04:22:14

标签: c# .net winforms datagridview

在这种情况下,有40个链接。但是在运行程序时,我发现它只在最后一行中添加了一行。

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);
            }
        }

在countriesCodes中有40个项目。

Datagridview

第一个问题是,它只添加一个国家和它的链接,它是最后一个,它不会添加其余的。

第二个问题是如何让它向右移动?我的意思是在运行时我正在使用鼠标将链接列向右拖动到右侧的datagridview端侧。我怎样才能做到正确的一面?我将其拖到右侧后添加了屏幕截图:

Link Data column to the right

还有第三个问题如何从国家/地区删除突出显示?我不希望它突出显示任何内容。现在,在运行程序时,该国家/地区的重点是自动显示。

Upodate

解决了我遇到的所有问题:

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;

1 个答案:

答案 0 :(得分:1)

        for (int i = 0; i < countriesCodes.Length; i++)
        {
            dataGridView1.Columns.Clear();

您正在清理列40次,因此只有循环的最后一次迭代才会显示任何内容。在for循环之前设置所有dataGridView1.Columns。 for循环应该只添加Rows。