按列 C# 填充数据表
Filling DataTable column by column C#
我正在尝试用动态创建的控件中的值填充 DataTable。想法是在DataTable 中有三列,并依次填充它们。首先填写第 1 列,然后是第 2 列,然后是第 3 列。但是,我很难有效地做到这一点。
发生的事情是正在填充 DataTable,但是第 1、2 和 3 列中的数据不是并排的,而是被填充在不同的行中。我知道这是因为我在每个循环中都添加了一行,但是我尝试了一些其他的方法但没有用。你能帮帮我吗?
DataTable tvpIngredients = new DataTable();
tvpIngredients.Columns.Add("Quantity", typeof(string));
tvpIngredients.Columns.Add("Measure", typeof(string));
tvpIngredients.Columns.Add("Ingredient", typeof(string));
foreach (Control ctrlQtt in quantity.Controls)
{
if (ctrlQtt is TextBox)
{
DataRow drNew = tvpIngredients.NewRow();
TextBox quantity = (TextBox)ctrlQtt;
drNew["Quantity"] = quantity.Text;
tvpIngredients.Rows.Add(drNew);
}
}
foreach (Control ctrlMsr in measure.Controls)
{
if (ctrlMsr is DropDownList)
{
DataRow drNew = tvpIngredients.NewRow();
DropDownList measure = (DropDownList)ctrlMsr;
drNew["Measure"] = measure.SelectedValue.ToString();
tvpIngredients.Rows.Add(drNew);
}
}
foreach (Control ctrlIng in ingredient.Controls)
{
if (ctrlIng is TextBox)
{
DataRow drNew = tvpIngredients.NewRow();
TextBox ingredient = (TextBox)ctrlIng;
drNew["Ingredient"] = ingredient.Text;
tvpIngredients.Rows.Add(drNew);
}
}
谢谢!
您已经添加了行。您需要改为修改行列。
DataTable tvpIngredients = new DataTable();
tvpIngredients.Columns.Add("Quantity", typeof(string));
tvpIngredients.Columns.Add("Measure", typeof(string));
tvpIngredients.Columns.Add("Ingredient", typeof(string));
foreach (Control ctrlQtt in quantity.Controls)
{
if (ctrlQtt is TextBox)
{
DataRow drNew = tvpIngredients.NewRow();
TextBox quantity = (TextBox)ctrlQtt;
drNew["Quantity"] = quantity.Text;
tvpIngredients.Rows.Add(drNew);
}
}
for (int i = 1; i < tvpIngredients.Rows.Count; i++)
{
if (ctrlMsr is DropDownList)
{
DataRow drNew = tvpIngredients.NewRow();
DropDownList measure = (DropDownList)ctrlMsr;
drNew["Measure"] = measure.SelectedValue.ToString();
tvpIngredients.Rows[i][1].ToString() = drNew;
}
}
for (int i = 1; i < tvpIngredients.Rows.Count; i++)
{
if (ctrlIng is TextBox)
{
DataRow drNew = tvpIngredients.NewRow();
TextBox ingredient = (TextBox)ctrlIng;
drNew["Ingredient"] = ingredient.Text;
tvpIngredients.Rows[i][2].ToString() = drNew;
}
}
要解决这样的问题,您需要考虑数据建模的方式。
查看此处的代码,我可以猜测您有一个表单,我们称它为食谱,它至少具有 3 个控制数量、度量和成分的控件。这些控件可以包含任意数量的项目。看起来它们的长度应该相等,因为它们都与您食谱中的一项相关。
你可以这样做:
if(quantity.Controls.Count != measure.Controls.Count
|| quantity.Controls.Count != ingredient.Controls.Count)
{
throw new Exception("Invalid ingredients list");
}
var quantities = new Control[quantity.Controls.Count];
var measurements = new Control[measure.Controls.Count];
var ingredients = new Control[ingredient.Controls.Count];
quantity.Controls.CopyTo(quantities, 0);
measure.Controls.CopyTo(measurements , 0);
ingredient.Controls.CopyTo(ingredients , 0);
for(var i = 0; i < quantity.Length; ++i)
{
// Make your new row
// index the arrays and fill the row data in
}
如果您花一些时间思考如何建模,可能会有更优雅的解决方案来解决整个问题。
我正在尝试用动态创建的控件中的值填充 DataTable。想法是在DataTable 中有三列,并依次填充它们。首先填写第 1 列,然后是第 2 列,然后是第 3 列。但是,我很难有效地做到这一点。
发生的事情是正在填充 DataTable,但是第 1、2 和 3 列中的数据不是并排的,而是被填充在不同的行中。我知道这是因为我在每个循环中都添加了一行,但是我尝试了一些其他的方法但没有用。你能帮帮我吗?
DataTable tvpIngredients = new DataTable();
tvpIngredients.Columns.Add("Quantity", typeof(string));
tvpIngredients.Columns.Add("Measure", typeof(string));
tvpIngredients.Columns.Add("Ingredient", typeof(string));
foreach (Control ctrlQtt in quantity.Controls)
{
if (ctrlQtt is TextBox)
{
DataRow drNew = tvpIngredients.NewRow();
TextBox quantity = (TextBox)ctrlQtt;
drNew["Quantity"] = quantity.Text;
tvpIngredients.Rows.Add(drNew);
}
}
foreach (Control ctrlMsr in measure.Controls)
{
if (ctrlMsr is DropDownList)
{
DataRow drNew = tvpIngredients.NewRow();
DropDownList measure = (DropDownList)ctrlMsr;
drNew["Measure"] = measure.SelectedValue.ToString();
tvpIngredients.Rows.Add(drNew);
}
}
foreach (Control ctrlIng in ingredient.Controls)
{
if (ctrlIng is TextBox)
{
DataRow drNew = tvpIngredients.NewRow();
TextBox ingredient = (TextBox)ctrlIng;
drNew["Ingredient"] = ingredient.Text;
tvpIngredients.Rows.Add(drNew);
}
}
谢谢!
您已经添加了行。您需要改为修改行列。
DataTable tvpIngredients = new DataTable();
tvpIngredients.Columns.Add("Quantity", typeof(string));
tvpIngredients.Columns.Add("Measure", typeof(string));
tvpIngredients.Columns.Add("Ingredient", typeof(string));
foreach (Control ctrlQtt in quantity.Controls)
{
if (ctrlQtt is TextBox)
{
DataRow drNew = tvpIngredients.NewRow();
TextBox quantity = (TextBox)ctrlQtt;
drNew["Quantity"] = quantity.Text;
tvpIngredients.Rows.Add(drNew);
}
}
for (int i = 1; i < tvpIngredients.Rows.Count; i++)
{
if (ctrlMsr is DropDownList)
{
DataRow drNew = tvpIngredients.NewRow();
DropDownList measure = (DropDownList)ctrlMsr;
drNew["Measure"] = measure.SelectedValue.ToString();
tvpIngredients.Rows[i][1].ToString() = drNew;
}
}
for (int i = 1; i < tvpIngredients.Rows.Count; i++)
{
if (ctrlIng is TextBox)
{
DataRow drNew = tvpIngredients.NewRow();
TextBox ingredient = (TextBox)ctrlIng;
drNew["Ingredient"] = ingredient.Text;
tvpIngredients.Rows[i][2].ToString() = drNew;
}
}
要解决这样的问题,您需要考虑数据建模的方式。 查看此处的代码,我可以猜测您有一个表单,我们称它为食谱,它至少具有 3 个控制数量、度量和成分的控件。这些控件可以包含任意数量的项目。看起来它们的长度应该相等,因为它们都与您食谱中的一项相关。
你可以这样做:
if(quantity.Controls.Count != measure.Controls.Count
|| quantity.Controls.Count != ingredient.Controls.Count)
{
throw new Exception("Invalid ingredients list");
}
var quantities = new Control[quantity.Controls.Count];
var measurements = new Control[measure.Controls.Count];
var ingredients = new Control[ingredient.Controls.Count];
quantity.Controls.CopyTo(quantities, 0);
measure.Controls.CopyTo(measurements , 0);
ingredient.Controls.CopyTo(ingredients , 0);
for(var i = 0; i < quantity.Length; ++i)
{
// Make your new row
// index the arrays and fill the row data in
}
如果您花一些时间思考如何建模,可能会有更优雅的解决方案来解决整个问题。