关于数据显示在repeater控件的有关问题,想让数据显示在对应的列里,请大神指教,
关于数据显示在repeater控件的问题,想让数据显示在对应的列里,请大神指教,,,,,
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable fData = null;
fData = new DataTable("TEST");
fData.Columns.Add("编号");
fData.Columns.Add("品名");
fData.Columns.Add("型号");
fData.Columns.Add("尺寸");
fData.Columns.Add("规格");
fData.Columns.Add("采购数量");
fData.Columns.Add("入库数量");
for (int i = 0; i < 1; i++)
{
fData.Rows.Add(new object[] { string.Empty, string.Empty });
}
rpt.DataSource = fData;
rpt.DataBind();
}
}
protected void rpt_ItemCommand(object source, RepeaterCommandEventArgs e)
{
Console.Write(e.Item);
}
private int fButtonIndex = -1;
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DataRowView dr = e.Item.DataItem as DataRowView;
if (dr == null)
{
return;
}
fButtonIndex++;
((TextBox)e.Item.FindControl("sku")).Text = dr[0].ToString();
((TextBox)e.Item.FindControl("name")).Text = dr[1].ToString();
((TextBox)e.Item.FindControl("model")).Text = dr[2].ToString();
((TextBox)e.Item.FindControl("size")).Text = dr[3].ToString();
((TextBox)e.Item.FindControl("spec")).Text = dr[4].ToString();
((TextBox)e.Item.FindControl("pur_qty")).Text = dr[5].ToString();
((TextBox)e.Item.FindControl("cin_qty")).Text = dr[6].ToString();
var deleteButton = e.Item.FindControl("btnDelete") as Button;
deleteButton.Attributes.Add("index", fButtonIndex.ToString());
var sku_changed = e.Item.FindControl("sku") as TextBox;
sku_changed.Attributes.Add("index", fButtonIndex.ToString());
}
protected void ButtonDelete_Click(object sender, EventArgs e)
{
DataTable fData = null;
fData = new DataTable("TEST");
fData.Columns.Add("编号");
fData.Columns.Add("品名");
fData.Columns.Add("型号");
fData.Columns.Add("尺寸");
fData.Columns.Add("规格");
fData.Columns.Add("采购数量");
fData.Columns.Add("入库数量");
var toDeleteRowIndex = int.Parse(((Button)sender).Attributes["index"]);
foreach (RepeaterItem ri in rpt.Items)
{
DataRow dr = fData.NewRow();
dr[0] = ((TextBox)ri.FindControl("sku")).Text;
dr[1] = ((TextBox)ri.FindControl("name")).Text;
dr[2] = ((TextBox)ri.FindControl("model")).Text;
dr[3] = ((TextBox)ri.FindControl("size")).Text;
dr[4] = ((TextBox)ri.FindControl("spec")).Text;
dr[5] = ((TextBox)ri.FindControl("pur_qty")).Text;
dr[6] = ((TextBox)ri.FindControl("cin_qty")).Text;
fData.Rows.Add(dr);
}
fData.Rows.RemoveAt(toDeleteRowIndex);
rpt.DataSource = fData;
rpt.DataBind();
}
protected void Button3_Click(object sender, EventArgs e)
{
DataTable fData = GetRepeaterDataTable();
DataRow drNew = fData.NewRow();
drNew[0] = "";
drNew[1] = "";
drNew[2] = "";
drNew[3] = "";
drNew[4] = "";
drNew[5] = "";
drNew[6] = "";
fData.Rows.Add(drNew);
rpt.DataSource = fData;
rpt.DataBind();
}
private DataTable GetRepeaterDataTable()
{
DataTable fData = null;
fData = new DataTable("TEST");
fData.Columns.Add("编号");
fData.Columns.Add("品名");
fData.Columns.Add("型号");
fData.Columns.Add("尺寸");
fData.Columns.Add("规格");
fData.Columns.Add("采购数量");
fData.Columns.Add("入库数量");
foreach (RepeaterItem ri in rpt.Items)
{
DataRow dr = fData.NewRow();
dr[0] = ((TextBox)ri.FindControl("sku")).Text;
dr[1] = ((TextBox)ri.FindControl("name")).Text;
dr[2] = ((TextBox)ri.FindControl("model")).Text;
dr[3] = ((TextBox)ri.FindControl("size")).Text;
dr[4] = ((TextBox)ri.FindControl("spec")).Text;
dr[5] = ((TextBox)ri.FindControl("pur_qty")).Text;
dr[6] = ((TextBox)ri.FindControl("cin_qty")).Text;
fData.Rows.Add(dr);
}
return fData;
}[
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection myCon = null;
SqlCommand myCom = null;
SqlDataAdapter myDa = null;
DataSet myDs = null;
try
{
string myConStr = System.Configuration.ConfigurationSettings.AppSettings["db_link"]; ;
myCon = new SqlConnection(myConStr);
myCon.Open();
myCom = new SqlCommand();
myCom.Connection = myCon;
string strSelect = "select * from STO";
myCom.CommandType = CommandType.Text;
myCom.CommandText = strSelect;
myDa = new SqlDataAdapter();
myDa.SelectCommand = myCom;
myDs = new DataSet();
myDa.Fill(myDs, "DS_STO");
GridView1.DataSource = myDs.Tables["DS_STO"];
GridView1.DataBind();
}
catch (Exception oe)
{
}
finally
{
if (myCon != null)
{
myCon.Close();
}
}//try 结束
}
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
string id = TextBox2.Text.Trim();
SqlConnection myCon = null;
SqlCommand myCom = null;
SqlDataAdapter myDa = null;
DataSet myDs = null;
try
{
string myConStr = System.Configuration.ConfigurationSettings.AppSettings["db_link"]; ;
myCon = new SqlConnection(myConStr);
myCon.Open();
myCom = new SqlCommand();
myCom.Connection = myCon;
string strSelect = "select * from PUR_detail where id='{0}'";
string sql = string.Format(strSelect, id);
myCom.CommandType = CommandType.Text;
myCom.CommandText = sql;
myDa = new SqlDataAdapter();
myDa.SelectCommand = myCom;
myDs = new DataSet();
myDa.Fill(myDs, "DS_PUR_detail");
this.rpt.DataSource = myDs.Tables[0].DefaultView; //myDs.Tables["DS_PUR_detail"];
this.rpt.DataBind();
}
catch (Exception oe)
{
}
finally
{
if (myCon != null)
{
myCon.Close();
}
}//try 结束
}
------解决思路----------------------
没看懂这么多代码与题目关系
------解决思路----------------------
你这个已经对应了:
((TextBox)e.Item.FindControl("sku")).Text = dr[0].ToString();
((TextBox)e.Item.FindControl("name")).Text = dr[1].ToString();
((TextBox)e.Item.FindControl("model")).Text = dr[2].ToString();
((TextBox)e.Item.FindControl("size")).Text = dr[3].ToString();
((TextBox)e.Item.FindControl("spec")).Text = dr[4].ToString();
((TextBox)e.Item.FindControl("pur_qty")).Text = dr[5].ToString();
((TextBox)e.Item.FindControl("cin_qty")).Text = dr[6].ToString();
在这里面调试看哪步出错了
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable fData = null;
fData = new DataTable("TEST");
fData.Columns.Add("编号");
fData.Columns.Add("品名");
fData.Columns.Add("型号");
fData.Columns.Add("尺寸");
fData.Columns.Add("规格");
fData.Columns.Add("采购数量");
fData.Columns.Add("入库数量");
for (int i = 0; i < 1; i++)
{
fData.Rows.Add(new object[] { string.Empty, string.Empty });
}
rpt.DataSource = fData;
rpt.DataBind();
}
}
protected void rpt_ItemCommand(object source, RepeaterCommandEventArgs e)
{
Console.Write(e.Item);
}
private int fButtonIndex = -1;
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DataRowView dr = e.Item.DataItem as DataRowView;
if (dr == null)
{
return;
}
fButtonIndex++;
((TextBox)e.Item.FindControl("sku")).Text = dr[0].ToString();
((TextBox)e.Item.FindControl("name")).Text = dr[1].ToString();
((TextBox)e.Item.FindControl("model")).Text = dr[2].ToString();
((TextBox)e.Item.FindControl("size")).Text = dr[3].ToString();
((TextBox)e.Item.FindControl("spec")).Text = dr[4].ToString();
((TextBox)e.Item.FindControl("pur_qty")).Text = dr[5].ToString();
((TextBox)e.Item.FindControl("cin_qty")).Text = dr[6].ToString();
var deleteButton = e.Item.FindControl("btnDelete") as Button;
deleteButton.Attributes.Add("index", fButtonIndex.ToString());
var sku_changed = e.Item.FindControl("sku") as TextBox;
sku_changed.Attributes.Add("index", fButtonIndex.ToString());
}
protected void ButtonDelete_Click(object sender, EventArgs e)
{
DataTable fData = null;
fData = new DataTable("TEST");
fData.Columns.Add("编号");
fData.Columns.Add("品名");
fData.Columns.Add("型号");
fData.Columns.Add("尺寸");
fData.Columns.Add("规格");
fData.Columns.Add("采购数量");
fData.Columns.Add("入库数量");
var toDeleteRowIndex = int.Parse(((Button)sender).Attributes["index"]);
foreach (RepeaterItem ri in rpt.Items)
{
DataRow dr = fData.NewRow();
dr[0] = ((TextBox)ri.FindControl("sku")).Text;
dr[1] = ((TextBox)ri.FindControl("name")).Text;
dr[2] = ((TextBox)ri.FindControl("model")).Text;
dr[3] = ((TextBox)ri.FindControl("size")).Text;
dr[4] = ((TextBox)ri.FindControl("spec")).Text;
dr[5] = ((TextBox)ri.FindControl("pur_qty")).Text;
dr[6] = ((TextBox)ri.FindControl("cin_qty")).Text;
fData.Rows.Add(dr);
}
fData.Rows.RemoveAt(toDeleteRowIndex);
rpt.DataSource = fData;
rpt.DataBind();
}
protected void Button3_Click(object sender, EventArgs e)
{
DataTable fData = GetRepeaterDataTable();
DataRow drNew = fData.NewRow();
drNew[0] = "";
drNew[1] = "";
drNew[2] = "";
drNew[3] = "";
drNew[4] = "";
drNew[5] = "";
drNew[6] = "";
fData.Rows.Add(drNew);
rpt.DataSource = fData;
rpt.DataBind();
}
private DataTable GetRepeaterDataTable()
{
DataTable fData = null;
fData = new DataTable("TEST");
fData.Columns.Add("编号");
fData.Columns.Add("品名");
fData.Columns.Add("型号");
fData.Columns.Add("尺寸");
fData.Columns.Add("规格");
fData.Columns.Add("采购数量");
fData.Columns.Add("入库数量");
foreach (RepeaterItem ri in rpt.Items)
{
DataRow dr = fData.NewRow();
dr[0] = ((TextBox)ri.FindControl("sku")).Text;
dr[1] = ((TextBox)ri.FindControl("name")).Text;
dr[2] = ((TextBox)ri.FindControl("model")).Text;
dr[3] = ((TextBox)ri.FindControl("size")).Text;
dr[4] = ((TextBox)ri.FindControl("spec")).Text;
dr[5] = ((TextBox)ri.FindControl("pur_qty")).Text;
dr[6] = ((TextBox)ri.FindControl("cin_qty")).Text;
fData.Rows.Add(dr);
}
return fData;
}[
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection myCon = null;
SqlCommand myCom = null;
SqlDataAdapter myDa = null;
DataSet myDs = null;
try
{
string myConStr = System.Configuration.ConfigurationSettings.AppSettings["db_link"]; ;
myCon = new SqlConnection(myConStr);
myCon.Open();
myCom = new SqlCommand();
myCom.Connection = myCon;
string strSelect = "select * from STO";
myCom.CommandType = CommandType.Text;
myCom.CommandText = strSelect;
myDa = new SqlDataAdapter();
myDa.SelectCommand = myCom;
myDs = new DataSet();
myDa.Fill(myDs, "DS_STO");
GridView1.DataSource = myDs.Tables["DS_STO"];
GridView1.DataBind();
}
catch (Exception oe)
{
}
finally
{
if (myCon != null)
{
myCon.Close();
}
}//try 结束
}
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
string id = TextBox2.Text.Trim();
SqlConnection myCon = null;
SqlCommand myCom = null;
SqlDataAdapter myDa = null;
DataSet myDs = null;
try
{
string myConStr = System.Configuration.ConfigurationSettings.AppSettings["db_link"]; ;
myCon = new SqlConnection(myConStr);
myCon.Open();
myCom = new SqlCommand();
myCom.Connection = myCon;
string strSelect = "select * from PUR_detail where id='{0}'";
string sql = string.Format(strSelect, id);
myCom.CommandType = CommandType.Text;
myCom.CommandText = sql;
myDa = new SqlDataAdapter();
myDa.SelectCommand = myCom;
myDs = new DataSet();
myDa.Fill(myDs, "DS_PUR_detail");
this.rpt.DataSource = myDs.Tables[0].DefaultView; //myDs.Tables["DS_PUR_detail"];
this.rpt.DataBind();
}
catch (Exception oe)
{
}
finally
{
if (myCon != null)
{
myCon.Close();
}
}//try 结束
}
------解决思路----------------------
没看懂这么多代码与题目关系
------解决思路----------------------
你这个已经对应了:
((TextBox)e.Item.FindControl("sku")).Text = dr[0].ToString();
((TextBox)e.Item.FindControl("name")).Text = dr[1].ToString();
((TextBox)e.Item.FindControl("model")).Text = dr[2].ToString();
((TextBox)e.Item.FindControl("size")).Text = dr[3].ToString();
((TextBox)e.Item.FindControl("spec")).Text = dr[4].ToString();
((TextBox)e.Item.FindControl("pur_qty")).Text = dr[5].ToString();
((TextBox)e.Item.FindControl("cin_qty")).Text = dr[6].ToString();
在这里面调试看哪步出错了