以编程方式创建网格 [英] Creating a Grid Programmatically
问题描述
我对.NET还是陌生的,所以请原谅我的冗长描述.我需要创建一个基于用户输入的对象.例如.如果用户输入5,则对象将有5行;否则,行将变为5.每行将有五列,四个文本框(一个带有TextMode = MultiLine)和一个下拉列表(DDL).
我应该使用数据绑定控件吗?如果是这样,我该如何动态控制行数?或者,我是否动态创建对象? (如何创建该对象并使之可见?)一旦用户确认了数据,则必须将其保存到SQL表中;否则,将其保存到SQL表中.我要将所选内容的参考ID保存在DDL中.
因为我还没有掌握编码,所以我只能提供目标的一个示例:
用户指示有15人要雇用.有4个组:工人,主管,经理和董事.分发是通过一种方法确定的,其中DDL和多行文本框是可编辑的.因此,将有9名工人,3名主管,2名经理和1名董事.每个组将具有以下内容:标题,职位描述,总数,位置(DDL)和注释(多行文本).
位置和注释可由用户编辑.该位置将从包含以下内容的SQL Server参考表中填充:办公室,一楼,前门,后门和屋顶.
感谢您的帮助.
I am new to the world of .NET so please excuse my lenghty descriptions. I need to create an object that is based upon user input. E.G. If the user enters 5, then the object will have 5 rows; each row will have five columns, four text boxes (one with TextMode=MultiLine) and one drop-down list (DDL).
Should I use a data-bound control? If so, which one and how do I dynamically control the number of rows? Or, do I create an object dynamically? (How do I create and make that object visible?) Once the user confirms the data, it must be then saved to a SQL table; I want save the reference id of the selection in the DDL.
Because I haven''t master coding yet, I can only provide an example of the objective:
User indicates that there are 15 people to hire. There are 4 groups: Worker, Supervisor, Manager and Director. The distribution is determined by a method, with the DDL and multi-line text box editable. So, there will be 9 workers, 3 supervisors, 2 managers and 1 director. Each group will have the following: Title, Job Description, Total Number, Location (DDL) and Notes (MultiLine Text).
The Location and Notes are editable by the user. The Location will be populated from a SQL Server reference table containing: Office, First Floor, Front Door, Back Door and Rooftop.
I appreciate the assistance.
推荐答案
使用以下代码:-
Use following code:-
<div>
<asp:Label ID="lblRowReq" Text="Required rows:" runat="server"></asp:Label>
<asp:TextBox ID="txtRowReq" runat="server"></asp:TextBox>
<asp:Button ID="btnRowGenrate" runat="server" Text="Generate Row"
onclick="btnRowGenrate_Click" />
</div>
<div>
<asp:gridview ID="gvGenrateRow" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Normal Text1">
<ItemTemplate>
<asp:TextBox ID="TextBox0" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Normal Text2">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Normal Text3">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Multiline Text">
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DropdownList">
<ItemTemplate>
<asp:DropDownList ID="ddlCategory" runat="server">
<asp:ListItem Text="A"></asp:ListItem>
<asp:ListItem Text="D"></asp:ListItem>
<asp:ListItem Text="B"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
</div>
在.cs页上使用以下代码:
and on .cs page use this:
private void SetFirstRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Column0", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dt.Columns.Add(new DataColumn("Column4", typeof(string)));
dr = dt.NewRow();
dr["Column0"] = string.Empty;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dr["Column3"] = string.Empty;
dr["Column4"] = string.Empty;
dt.Rows.Add(dr);
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
gvGenrateRow.DataSource = dt;
gvGenrateRow.DataBind();
}
protected void btnRowGenrate_Click(object sender, EventArgs e)
{
SetFirstRow();
for (int i = 0; i < Convert.ToInt32(txtRowReq.Text)-1; i++)
AddNewRowToGrid();
}
private void AddNewRowToGrid()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
TextBox box0 = (TextBox)gvGenrateRow.Rows[rowIndex].Cells[0].FindControl("TextBox0");
TextBox box1 = (TextBox)gvGenrateRow.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)gvGenrateRow.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)gvGenrateRow.Rows[rowIndex].Cells[3].FindControl("TextBox3");
DropDownList ddl4 = (DropDownList)gvGenrateRow.Rows[rowIndex].Cells[4].FindControl("ddlCategory");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["Column0"] = box0.Text;
drCurrentRow["Column1"] = box1.Text;
drCurrentRow["Column2"] = box2.Text;
drCurrentRow["Column3"] = box3.Text;
drCurrentRow["Column4"] = ddl4.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
gvGenrateRow.DataSource = dtCurrentTable;
gvGenrateRow.DataBind();
}
}
}
如果您尝试逐步解决问题,那么它会更好,因此第一步已完成,请尝试下一个.... happy编码:-) jmd.
if you try to solve your problem step by step then its more better,so first step is completed now try for next....happy coding:-) jmd.
这篇关于以编程方式创建网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!