我正在开发 Windows 窗体应用程序。我创建了具有 datagridview 控件的用户控件。我在一个面板(主要形式)中动态添加了 10 个用户控件,该面板具有共同的“已观察(秒)”列。在此列中,用户将输入值。然后单击保存按钮。所有值都将保存在文本文件中。
我的问题是:我每次在面板中添加一个新的用户控件。那么如何从 Observed 列中获取值以保存按钮?
主窗体代码
private void addUserContol()
{
string[] lines = File.ReadAllLines(filename);
string[] newline;
string[] splitLine;
try
{
int i = 0;
string lineText;
for (i = 0; i < lines.Length; )
{
uc_protectionTbl1 objUC = new uc_protectionTbl1(); //user control object is created here
uc_groupLbl objGrouplbl = new uc_groupLbl();
uc_testResult result = new uc_testResult();
uc_performResult objperform = new uc_performResult();
var wordCount = new Dictionary<string, int>();
lineText = lines[i++];
if (lineText != "")
{
if (lineText.Contains("Title"))
{
splitLine = lineText.Split('=');
lbl_title.Text = splitLine[1];
}
if (lineText.Contains("GroupLabel"))
{
splitLine = lineText.Split('=');
objGrouplbl.grouplblvalue = splitLine[1];
panelUC.Controls.Add(objGrouplbl);
}
if (lineText.Contains("Performance Test"))
{
panelUC.Controls.Add(objperform);
}
if (lineText.Contains("Result"))
{
splitLine = lineText.Split('=');
result.addTest = splitLine[1];
panelUC.Controls.Add(result);
}
if (lineText.Contains("TestType"))
{
splitLine = lineText.Split('=');
objUC.testType = splitLine[1];
lineText = lines[i++];
if (lineText.Contains("Heading"))
{
string[] delimiter = { "=", "||" };
splitLine = lineText.Split(delimiter, StringSplitOptions.None);
newline = splitLine.Skip(1).ToArray();
objUC.numColumn = newline.Count();
objUC.columnheading = newline;
}
while (i < lines.Length)
{
lineText = lines[i++];
if (lineText != "")
{
if (lineText.Contains("Value"))
{
string[] delimiter = { "=", "||" };
wordCount.Add(lineText, 1);
objUC.numRow = wordCount.Values.Count();
string[][] arrayofarray = wordCount.Select(x => (x.Key.Split(delimiter, StringSplitOptions.None)).Skip(1).ToArray()).ToArray();
objUC.rowValues = arrayofarray;
}
else
{
panelUC.Controls.Add(objUC); //here I added user control in panel
i = i - 1;
break;
}
}
}
}
}
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
private void SavetoolStripMenuItem1_Click(object sender, EventArgs e)
{
//Here I want all the values from observed(Sec) column
}
}
用户控制代码
public partial class uc_protectionTbl1 : UserControl
{
public string testType { get; set; }
public int numRow { get; set; }
public int numColumn { get; set; }
public string[] columnheading { get; set; }
public string[][] rowValues { get; set; }
public uc_protectionTbl1()
{
InitializeComponent();
}
private void uc_ProtectionTbl1_Load(object sender, EventArgs e)
{
designDT();
}
public DataTable uc_dt = new DataTable();
private void designDT()
{
try
{
foreach (var column in columnheading)
{
uc_dt.Columns.Add(new DataColumn(column.ToString()));
}
foreach (var row in rowValues)
{
uc_dt.Rows.Add(row);
}
dgv_uc.DataSource = uc_dt;
dgv_uc.Columns.Cast<DataGridViewColumn>().ToList().
ForEach(f => f.SortMode = DataGridViewColumnSortMode.NotSortable);
lbl_testType.Text = testType;
DataGridViewTextBoxColumn obs = new DataGridViewTextBoxColumn(); //Here I added textbox column
obs.HeaderText = "Observed(Sec)";
obs.Name = "Obs";
obs.ReadOnly = false;
this.dgv_uc.Columns.Add(obs);
var button = new DataGridViewButtonColumn();
button.Name = "TestButton";
button.HeaderText = "Test_Button";
button.Text = "Test";
button.UseColumnTextForButtonValue = true;
this.dgv_uc.Columns.Add(button);
sizeDGV(dgv_uc);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
请帮我解决这个问题。我是开发新手。从过去 15 天开始搜索谷歌,但没有得到正确的解决方案。提前致谢。