简体   繁体   中英

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

In this case there are 40 links. 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.

数据网格视图

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. 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. Do all of your dataGridView1.Columns setup before the for loop. The for loop should only be adding Rows.

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