我剛開始學習 C#,這是我在這里的第一個問題,我嘗試搜索答案,但我找不到我的問題的任何答案,或者我找到了一個但我無法在我的代碼中使用它,所以我想問你。
我正在為自己的作業制作小型應用程式,以將所有專案保存在一個地方,這是一個簡單的應用程式,因為我不太了解 C#,所以這就是我自己能夠制作的所有內容,但我卡在一點。這個應用程式是這樣作業的,我將專案名稱和鏈接粘貼到谷歌表(在我的公司中,我們使用模板,所以我想要獲取的值總是在相同的單元格中)然后應用程式獲取鏈接表所需的所有值并將它們寫入 txt檔案,然后它在dataGridView中顯示我需要的所有資料,并且一切正常,我能夠將所有值設定到正確的單元格中并且一切正常,如果我單擊單元格,我什至設法運行帶有鏈接的chrome,但所有鏈接都相當很長,我想縮短它們,它們看起來像這樣:
截屏
我試圖在按鈕中添加文本值,但是當我這樣做時它看不到鏈接并嘗試使用“文本”作為值并且鏈接不再起作用。
我的問題是:有什么方法可以縮短按鈕內的鏈接并使其保持作業狀態?我的意思是,如果我單擊按鈕,它將以正確的鏈接在 chrome 中啟動一個頁面,或者我也可以將單元格作為鏈接使用,如您在 SOW 列中看到的那樣,那么我想將整個鏈接縮短為“SOW”文本
感謝您的任何幫助
uj5u.com熱心網友回復:
假設你做了這樣的鏈接https://www.c-sharpcorner.com/blogs/set-content-of-grid-cell-as-hyperlink-in-datagridview-of-winform
您需要執行以下操作
- 將單元格的 Tag 屬性設定為 url。
- 將文本設定為您想要的任何內容
- 在點擊處理程式中
改變
System.Diagnostics.Process.Start("http://" dgv.CurrentCell.EditedFormattedValue);
到
System.Diagnostics.Process.Start("http://" dgv.CurrentCell.Tag);
uj5u.com熱心網友回復:
我以另一種方式做到了這一點,現在當我讀到你的帖子時,我不知道我是否有點搞砸了。這是我的代碼,它打開 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);
}
}
uj5u.com熱心網友回復:
使用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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/442253.html
下一篇:角色剛創建后為空
