Cragstor
New member
- Joined
- Aug 9, 2021
- Messages
- 3
- Programming Experience
- 1-3
Hello,
I'm new to here, so firstly hello everyone.
I have looked through the forums but can't find an answer and I'm struggling to find one online anywhere with how datasources work in this case.
The code below is a snippet of a program I am writing to transfer files. The problem I had is sometimes the file paths are extremely long and I wanted to shorten the presentation of the file path to the user.
To do this I thought I would just create a global DataTable variable and assign the value of the "real information" to this DataTable, before I reformat the cell text in the datagridview.
The problem I'm having is it seems to be referencing the datatables rather than assigning the values, because when I change the information in the cells of the datagridview (DgvMain) it is automatically updating my local DataTable (dt) and the global DataTable (activeDGVTable) with the trimmed strings.
I just want to assign to a DataTable and for it to not be linked to the datagridview, so that when I change the information in the datagridview, I can preserve what I stored in my global variable.
I hope I explained that in a way that can be understood.
For clarity, two things in there that I haven't referenced:
Any help would be greatly appreciated.
Craig
I'm new to here, so firstly hello everyone.
I have looked through the forums but can't find an answer and I'm struggling to find one online anywhere with how datasources work in this case.
The code below is a snippet of a program I am writing to transfer files. The problem I had is sometimes the file paths are extremely long and I wanted to shorten the presentation of the file path to the user.
To do this I thought I would just create a global DataTable variable and assign the value of the "real information" to this DataTable, before I reformat the cell text in the datagridview.
The problem I'm having is it seems to be referencing the datatables rather than assigning the values, because when I change the information in the cells of the datagridview (DgvMain) it is automatically updating my local DataTable (dt) and the global DataTable (activeDGVTable) with the trimmed strings.
I just want to assign to a DataTable and for it to not be linked to the datagridview, so that when I change the information in the datagridview, I can preserve what I stored in my global variable.
I hope I explained that in a way that can be understood.
For clarity, two things in there that I haven't referenced:
- I created an extension for string called Right which does nothing but gets the last X amount of characters from a string.
- SQLUtility.ExecuteQueryToDataTable handles my SQL call and it is a static class which returns a DataTable
Any help would be greatly appreciated.
Craig
C#:
public partial class TransferScreen : Form
{
DataTable activeDGVTable = new DataTable();
//function which loops through each cell in the datagridview and replaces it
//with the last 40 characters on the right
private void FormatDgvSourceDestination(DataGridView dgv)
{
foreach (DataGridViewRow row in dgv.Rows)
{
foreach(DataGridViewCell cell in row.Cells)
{
string cellString = cell.Value.ToString();
cell.Value = ".../" + cellString.Right(40);
UpdateLog(cell.Value.ToString(), Color.Orange);
}
}
}
//button to execute SQL query and assign the datagridview datesource and the global
//variable with the returned information
private void BtnReportCopyInstructions_Click(object sender, EventArgs e)
{
DataTable dt = SQLUtility.ExecuteQueryToDataTable("SQL CODE HERE");
DgvMain.DataSource = dt;
activeDGVTable = dt;
FormatDgvSourceDestination(DgvMain);
}
}