循环遍历 gridview 行后将字符串值添加到数组列表中 c#
Add string values into array list after looping through gridview rows c#
我正在使用具有批量更新功能的网格视图。在我对列进行更改并点击页面中的更新按钮后,将执行此 foreach 循环以进行更新。现在如何在循环后将这些字符串值添加到 arraylist
protected void btnUpdate_Click(object sender, EventArgs e)
{
try
{
foreach (GridViewRow row in gvDetails.Rows)
{
string strID = ((Label)row.FindControl("lblID")).Text;
string strGroup = ((Label)row.FindControl("lblGrp")).Text;
string strValue = ((TextBox)row.FindControl("txtValue")).Text;
}
ArrayList alList = new ArrayList();
alList.Add(strID);
alList.Add(strGroup);
alList.Add(strValue);
}
catch (Exception ex)
{
}
}
但是循环后的字符串值没有被插入到数组列表中。谁能帮我解决这个问题。
看起来你的代码应该是
var alList = new List<string>();
foreach (GridViewRow row in gvDetails.Rows)
{
string strID = ((Label)row.FindControl("lblID")).Text;
string strGroup = ((Label)row.FindControl("lblGrp")).Text;
string strValue = ((TextBox)row.FindControl("txtValue")).Text;
alList.Add(strID);
alList.Add(strGroup);
alList.Add(strValue);
}
否则你试图添加超出范围的列表变量(因为 strID
和其他已在循环内部范围内声明)
注意:不要使用 ArrayList
它已过时。在您的情况下,最好改用 List<string>
。
或者,如果我没有理解你的意思,而你需要添加到仅列出上次迭代的变量(因为它可以从你的代码中推测) - 然后声明 strID
和其他变量 在 循环之外,像这样:
string strID = null, strGroup = null, strValue = null;
var alList = new List<string>();
foreach (GridViewRow row in gvDetails.Rows)
{
strID = ((Label)row.FindControl("lblID")).Text;
strGroup = ((Label)row.FindControl("lblGrp")).Text;
strValue = ((TextBox)row.FindControl("txtValue")).Text;
}
alList.Add(strID);
alList.Add(strGroup);
alList.Add(strValue);
更新
由于我们已经在评论中阐明了您的目标,实际上您不需要 List<string>
而是 DataTable
,您的代码可能如下所示:
var dt = new DataTable();
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("Group", typeof(string));
dt.Columns.Add("Value", typeof(string));
foreach (GridViewRow row in gvDetails.Rows)
{
var dro = dt.NewRow();
dro["ID"] = ((Label)row.FindControl("lblID")).Text;
dro["Group"] = ((Label)row.FindControl("lblGrp")).Text;
dro["Value"] = ((TextBox)row.FindControl("txtValue")).Text;
dt.Rows.Add(dro);
}
另请注意 - 数据表列的数据类型可以是任何类型,而不仅仅是字符串 - 这取决于您要存储的实际数据。
您有一个 "scope"(您的变量)问题。在循环之前声明您的字符串。还有你的数组列表....错误...列表持有者对象也是如此。
try
{
List<string> alList = new List<string>();
string strID = string.Empty;
string strGroup = string.Empty;
string strValue = string.Empty;
foreach (GridViewRow row in gvDetails.Rows)
{
strID = ((Label)row.FindControl("lblID")).Text;
strGroup = ((Label)row.FindControl("lblGrp")).Text;
strValue = ((TextBox)row.FindControl("txtValue")).Text;
}
alList.Add(strID);
alList.Add(strGroup);
alList.Add(strValue);
}
catch (Exception ex)
{
}
追加:
我建议创建一个 DTO(数据传输对象)并填充它们,然后将其作为 object/collection 发送到将值保存到数据库的方法。
namespace MyApplication.Domain
{
[System.Diagnostics.DebuggerDisplay("ID = {ID}, GroupName = {GroupName}, Value='{Value}'")]
public partial class MyDto
{
public int ID { get; set; } /* PK */
public string GroupName { get; set; }
public string Value { get; set; }
}
}
try
{
List<MyDto> dtoCollection = new List<MyDto>();
foreach (GridViewRow row in gvDetails.Rows)
{
string strID = ((Label)row.FindControl("lblID")).Text;
string strGroup = ((Label)row.FindControl("lblGrp")).Text;
string strValue = ((TextBox)row.FindControl("txtValue")).Text;
MyDto newItem = new MyDto () {ID = Convert.ToInt32(strID), Group = strGroup, Value = strValue};
dtoCollection.Add(newItem);
}
int count = dtoCollection.Count;
/* now send your dtoCollection to your BusinessLayer ... have your businesslayer validate the input data......then have the business layer call your datalayer...and use the List<MyDto> objects to update your database */
}
catch (Exception ex)
{
}
我正在使用具有批量更新功能的网格视图。在我对列进行更改并点击页面中的更新按钮后,将执行此 foreach 循环以进行更新。现在如何在循环后将这些字符串值添加到 arraylist
protected void btnUpdate_Click(object sender, EventArgs e)
{
try
{
foreach (GridViewRow row in gvDetails.Rows)
{
string strID = ((Label)row.FindControl("lblID")).Text;
string strGroup = ((Label)row.FindControl("lblGrp")).Text;
string strValue = ((TextBox)row.FindControl("txtValue")).Text;
}
ArrayList alList = new ArrayList();
alList.Add(strID);
alList.Add(strGroup);
alList.Add(strValue);
}
catch (Exception ex)
{
}
}
但是循环后的字符串值没有被插入到数组列表中。谁能帮我解决这个问题。
看起来你的代码应该是
var alList = new List<string>();
foreach (GridViewRow row in gvDetails.Rows)
{
string strID = ((Label)row.FindControl("lblID")).Text;
string strGroup = ((Label)row.FindControl("lblGrp")).Text;
string strValue = ((TextBox)row.FindControl("txtValue")).Text;
alList.Add(strID);
alList.Add(strGroup);
alList.Add(strValue);
}
否则你试图添加超出范围的列表变量(因为 strID
和其他已在循环内部范围内声明)
注意:不要使用 ArrayList
它已过时。在您的情况下,最好改用 List<string>
。
或者,如果我没有理解你的意思,而你需要添加到仅列出上次迭代的变量(因为它可以从你的代码中推测) - 然后声明 strID
和其他变量 在 循环之外,像这样:
string strID = null, strGroup = null, strValue = null;
var alList = new List<string>();
foreach (GridViewRow row in gvDetails.Rows)
{
strID = ((Label)row.FindControl("lblID")).Text;
strGroup = ((Label)row.FindControl("lblGrp")).Text;
strValue = ((TextBox)row.FindControl("txtValue")).Text;
}
alList.Add(strID);
alList.Add(strGroup);
alList.Add(strValue);
更新
由于我们已经在评论中阐明了您的目标,实际上您不需要 List<string>
而是 DataTable
,您的代码可能如下所示:
var dt = new DataTable();
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("Group", typeof(string));
dt.Columns.Add("Value", typeof(string));
foreach (GridViewRow row in gvDetails.Rows)
{
var dro = dt.NewRow();
dro["ID"] = ((Label)row.FindControl("lblID")).Text;
dro["Group"] = ((Label)row.FindControl("lblGrp")).Text;
dro["Value"] = ((TextBox)row.FindControl("txtValue")).Text;
dt.Rows.Add(dro);
}
另请注意 - 数据表列的数据类型可以是任何类型,而不仅仅是字符串 - 这取决于您要存储的实际数据。
您有一个 "scope"(您的变量)问题。在循环之前声明您的字符串。还有你的数组列表....错误...列表持有者对象也是如此。
try
{
List<string> alList = new List<string>();
string strID = string.Empty;
string strGroup = string.Empty;
string strValue = string.Empty;
foreach (GridViewRow row in gvDetails.Rows)
{
strID = ((Label)row.FindControl("lblID")).Text;
strGroup = ((Label)row.FindControl("lblGrp")).Text;
strValue = ((TextBox)row.FindControl("txtValue")).Text;
}
alList.Add(strID);
alList.Add(strGroup);
alList.Add(strValue);
}
catch (Exception ex)
{
}
追加:
我建议创建一个 DTO(数据传输对象)并填充它们,然后将其作为 object/collection 发送到将值保存到数据库的方法。
namespace MyApplication.Domain
{
[System.Diagnostics.DebuggerDisplay("ID = {ID}, GroupName = {GroupName}, Value='{Value}'")]
public partial class MyDto
{
public int ID { get; set; } /* PK */
public string GroupName { get; set; }
public string Value { get; set; }
}
}
try
{
List<MyDto> dtoCollection = new List<MyDto>();
foreach (GridViewRow row in gvDetails.Rows)
{
string strID = ((Label)row.FindControl("lblID")).Text;
string strGroup = ((Label)row.FindControl("lblGrp")).Text;
string strValue = ((TextBox)row.FindControl("txtValue")).Text;
MyDto newItem = new MyDto () {ID = Convert.ToInt32(strID), Group = strGroup, Value = strValue};
dtoCollection.Add(newItem);
}
int count = dtoCollection.Count;
/* now send your dtoCollection to your BusinessLayer ... have your businesslayer validate the input data......then have the business layer call your datalayer...and use the List<MyDto> objects to update your database */
}
catch (Exception ex)
{
}