[英]Why I am not getting value of last row of editable column of datagridview in C# winForms application?
I created user control which has datagridview control. 我创建了具有datagridview控件的用户控件。 And datagridview is getting data from textfile to create row and columns dynamically. 而且datagridview从文本文件获取数据以动态创建行和列。 I added that user control to panel. 我将该用户控件添加到面板中。 One column of dgv is editable. dgv的一列是可编辑的。 I want that editable column values in a list. 我想要列表中的可编辑列值。 I am getting all the values except last row value. 我正在获取除最后一行值以外的所有值。
Code for main Form 主体代码
public static List<string> testValue = new List<string>();
private void SavetoolStripMenuItem1_Click(object sender, EventArgs e)
{
List<string> dummy = new List<string>();
foreach (Control p in panelUC.Controls)
{
if (p is uc_protectionTbl1)
{
dummy = ((((uc_protectionTbl1)p).obsValue));
testValue.AddRange(dummy); //here I want all values
}
if (p is uc_performResult)
{
}
}
Code for userControl userControl的代码
private List<string> listObs = new List<string>();
private string obs = null;
public List<string> obsValue
{
get
{
foreach (DataGridViewRow row in dgv_uc.Rows)
{
obs = row.Cells["Observed(Sec)"].Value.ToString();
listObs.Add(obs);
}
return listObs;
}
}
In testValue list index I am getting these values
[0] = "1";
[1] = "2";
[2] = "3";
[3] = ""; //here I want "4"
And listObs list I am getting following values
[0] = "3";
[1] = ""; //here I also want "4"
My TextFile from where I designed dgv
TestType=Phase Loset Over Current Protection Test (I>)
Heading=SrNo||Injected Current||Rated
Current||Char||Pickup(xIp)||TMS||STD]||Observed(Sec)
Value=Test-1||0.4||1||NINV3.0||0.2||0.1||0.95-1.00-1.05
Value=Test-2||7.0||1||NINV3.0||1.0||0.5||1.68-1.76-1.85
TestType=Earth Lowset Over Current Protection Test (Ie>)
Heading=SrNo||Injected Current||Rated
Current||Char||Pickup(xIn)||TMS||STD||Observed(Sec)
Value=Test-3||0.2||1||NINV3.0||0.1||0.1||0.95-1.00-1.05
Value=Test-4||7.0||1||NINV3.0||1.0||0.5||1.68-1.76-1.85
When I read "SrNo" column instead of "Observed(Sec)" column I got all the value from SrNo column. 当我读取“ SrNo”列而不是“ Observed(Sec)”列时,我从SrNo列获取了所有值。 Then why i am not able to read "Observed(Sec)" column which is editable? 那么,为什么我无法读取可编辑的“已观察(秒)”列?
I am not able to understand what I am missing. 我无法理解我所缺少的。 Please help me to resolve this issue. 请帮助我解决此问题。 Thanks in advance!! 提前致谢!!
You know that your getter gets called everytime you access the member, which means with your given code - everytime you access the list it will get bigger and bigger - since you aren't clearing or re-creating the list upon access so it grows indefinetly. 您知道每次访问成员时都会调用getter,这意味着使用给定的代码-每次访问列表时,它都会越来越大-因为您不会在访问时清除或重新创建列表,因此它会无限期地增长。 - I'd suggest fix that issue first than check your values again. -建议您先解决该问题,然后再检查您的值。
Change your member obsValue
to this: 将您的成员obsValue
更改为此:
//remove the fields obsValue and obs, and use local variables instead
//private List<string> listObs = new List<string>();
//private string obs = null;
public List<string> obsValue
{
get
{
//create a new list everytime the getter of this member gets called
var listObs = new List<string>();
foreach (DataGridViewRow row in dgv_uc.Rows)
{
//declare obs here since we only use it here
var obs = row.Cells["Observed(Sec)"].Value.ToString();
listObs.Add(obs);
}
return listObs;
}
}
Try the for loop instead 尝试使用for循环
for (int i = 0; i < dgv_us.Rows.Count(); i++)
{
string obs = dgv_uc.Rows[i].Cells["Observed(Sec)"].Value.ToString();
listObs.Add(obs);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.