Home > Blockchain >  How to shorten a link in a DataGridView?
How to shorten a link in a DataGridView?

Time:03-22

I'm making for myself small app to my work to keep in one place all my projects.
This app works like this, I paste project name and link to google sheet (in my company we use template so values I want to get are always in the same cells) then app gets all values I need for linked sheet and writes them inside txt file, then it shows all data I need in DataGridView and all works perfect, I'm able to set all values into correct cells.
I even managed to run chrome with link if I click in cell but all links are quite long and I would like to shorten them, they look like this:

screenshot

I tried to add text value in button but when I do this it doesn't see link and tries to use "Text" as a value and link is no longer working.

My question is: is there any way to shorten link inside button and still keep it working ? I mean if I click on button it will start a page in chrome with proper link OR I also can work with cells as links as you can see in SOW column so then I would like to shorten whole link into "SOW" text

CodePudding user response:

Assuming that you did the links like this https://www.c-sharpcorner.com/blogs/set-content-of-grid-cell-as-hyperlink-in-datagridview-of-winform

you need to do the following

  • set the Tag property of the cell to the url.
  • set the text to whatever you want
  • in the click handler

change

  System.Diagnostics.Process.Start("http://"   dgv.CurrentCell.EditedFormattedValue); 

to

 System.Diagnostics.Process.Start("http://"   dgv.CurrentCell.Tag); 

CodePudding user response:

I've done this in another way and now when I'he read your post I don't know if I didn't messed up a little. This is my code which opens txt file, reads lines separate data and adds new row for each line if it isn't empty

        public void refresh()
    {
        dataGridView1.Rows.Clear();
        string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"LayoutElements\dataBase.txt");
        var Lines = File.ReadAllLines(path);
        int linesCount = Lines.Count();

        if (linesCount == 0)
        {
        }
        else
        {
            int lineNumber = 0;
            int rowNumber = 0;
            foreach (var line in Lines)
            {
                if (line == "")
                {
                    lineNumber  ;
                }
                else
                {
                    string info = Lines[lineNumber];
                    string[] splitedInfo = info.Split("///");

                    string projectName = splitedInfo[0];
                    string SOW = splitedInfo[2];
                    string refMat = splitedInfo[3];
                    string PDS = splitedInfo[4];
                    string QC = splitedInfo[5];
                    string Estimated = splitedInfo[6];
                    string Spent = splitedInfo[7];
                    string Efficiency = splitedInfo[8];
                    string Progress = splitedInfo[9];
                    string URL = splitedInfo[1];
                    dataGridView1.Rows.Insert(rowNumber, projectName, URL, SOW, refMat, PDS, QC, Estimated, Spent, Efficiency, Progress);
                    lineNumber  ;
                    rowNumber  ;
                }
            }
        }
    }

and then this is how my click handler looks like

        private void dataGridView1_CellContentClick(object pSender, DataGridViewCellEventArgs pArgs)
    {
        string dataFromCell = dataGridView1.CurrentCell.Value.ToString();

        if (dataFromCell.Contains("https://"))
        {
            string chromeLocation = @"C:\Program Files\Google\Chrome\Application\chrome.exe";
            Process.Start(chromeLocation, dataFromCell);
        }
    }

CodePudding user response:

Use the CellFormatting event to replace the long URL with a shortened version. For example:

    Form df = new Form { Size = new Size(800,800) };
    DataGridView dgv1 = new DataGridView { Dock = DockStyle.Fill };
    dgv1.Columns.Add(new DataGridViewLinkColumn { DataPropertyName = "URL", HeaderText = "URL" });
    dgv1.Rows.Add(new Object[] { new Uri("http://thisissomeverlongurlthatistakinguptoomuchwidth.com") });
    dgv1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
    df.Controls.Add(dgv1);
    dgv1.CellFormatting  = (o, e) => {
        if (e.ColumnIndex >= 0 && dgv1.Columns[e.ColumnIndex] is DataGridViewLinkColumn) {
            if (e.Value is Uri) {
                e.Value = "link"; // could also take a substring from the Uri
            }
        }
    };
    Application.Run(df);
  • Related