如何缩短 DataGridView 中的 link?
How to shorten a link in a DataGridView?
我正在为自己的工作制作一个小应用程序,以便将我的所有项目保存在一个地方。
这个应用程序是这样工作的,我将项目名称和 link 粘贴到 google sheet (在我的公司我们使用模板,所以我想要获取的值总是在相同的单元格中)然后应用程序获取我需要 linked sheet 的所有值并将它们写入 txt 文件,然后它在 DataGridView 中显示我需要的所有数据并且一切正常,我能够将所有值设置到正确的单元格中。
如果我在单元格中单击,我什至设法 运行 chrome 和 link 但所有 link 都很长,我想缩短它们,它们看起来像这样:
我尝试在按钮中添加文本值,但是当我这样做时它没有看到 link 并尝试使用“文本”作为值并且 link 不再有效。
我的问题是:有什么方法可以缩短 link 内部按钮并使其继续工作吗?我的意思是,如果我单击按钮,它会在 chrome 中开始一个页面,其中包含正确的 link 或者我也可以使用 links 的单元格,正如您在 SOW 列中看到的那样,那么我会喜欢将整个 link 缩短为“SOW”文本
假设您是这样链接的https://www.c-sharpcorner.com/blogs/set-content-of-grid-cell-as-hyperlink-in-datagridview-of-winform
您需要执行以下操作
- 将单元格的标签 属性 设置为 url。
- 将文本设置为您想要的任何内容
- 在点击处理程序中
改变
System.Diagnostics.Process.Start("http://" + dgv.CurrentCell.EditedFormattedValue);
到
System.Diagnostics.Process.Start("http://" + dgv.CurrentCell.Tag);
我用另一种方式做到了这一点,现在当我读到你的文章时 post 我不知道我是不是有点搞砸了。这是我的代码,它打开 txt 文件,读取行分隔数据并为每行添加新行(如果它不为空)
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++;
}
}
}
}
然后这就是我的点击处理程序的样子
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);
}
}
使用 CellFormatting
事件将长的 URL 替换为缩短的版本。例如:
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);
我正在为自己的工作制作一个小应用程序,以便将我的所有项目保存在一个地方。
这个应用程序是这样工作的,我将项目名称和 link 粘贴到 google sheet (在我的公司我们使用模板,所以我想要获取的值总是在相同的单元格中)然后应用程序获取我需要 linked sheet 的所有值并将它们写入 txt 文件,然后它在 DataGridView 中显示我需要的所有数据并且一切正常,我能够将所有值设置到正确的单元格中。
如果我在单元格中单击,我什至设法 运行 chrome 和 link 但所有 link 都很长,我想缩短它们,它们看起来像这样:
我尝试在按钮中添加文本值,但是当我这样做时它没有看到 link 并尝试使用“文本”作为值并且 link 不再有效。
我的问题是:有什么方法可以缩短 link 内部按钮并使其继续工作吗?我的意思是,如果我单击按钮,它会在 chrome 中开始一个页面,其中包含正确的 link 或者我也可以使用 links 的单元格,正如您在 SOW 列中看到的那样,那么我会喜欢将整个 link 缩短为“SOW”文本
假设您是这样链接的https://www.c-sharpcorner.com/blogs/set-content-of-grid-cell-as-hyperlink-in-datagridview-of-winform
您需要执行以下操作
- 将单元格的标签 属性 设置为 url。
- 将文本设置为您想要的任何内容
- 在点击处理程序中
改变
System.Diagnostics.Process.Start("http://" + dgv.CurrentCell.EditedFormattedValue);
到
System.Diagnostics.Process.Start("http://" + dgv.CurrentCell.Tag);
我用另一种方式做到了这一点,现在当我读到你的文章时 post 我不知道我是不是有点搞砸了。这是我的代码,它打开 txt 文件,读取行分隔数据并为每行添加新行(如果它不为空)
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++;
}
}
}
}
然后这就是我的点击处理程序的样子
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);
}
}
使用 CellFormatting
事件将长的 URL 替换为缩短的版本。例如:
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);