如何在gridview中绑定动态标头和列 [英] How to bind dynamic header and columns in gridview
问题描述
如何从gridview中的一个数据表绑定动态标头,动态第一列数据来自另一个数据表,其余列作为复选框。
你能告诉我吗?如何在动态下绑定这个下面的Senario。
用户| Test1 | Test2 | Test3 | Test4 | Test5 |
-------------------------------- -----
Name1 | Checkbox1 | Checkbox2 | CheckBox3 |
Name2 | Checkbox1 | Checkbox2 | Checkbox3
我尝试过:
source代码
how to bind dynamic headers from one data table in gridview and dynamic first column data is from another data table and remaining columns as checkboxes.
can u tell me how to bind this below senario in dynamic.
User | Test1|Test2|Test3|Test4|Test5|
-------------------------------------
Name1 | Checkbox1|Checkbox2|CheckBox3|
Name2 | Checkbox1|Checkbox2|Checkbox3
What I have tried:
source code
<asp:GridView ID="GridView1" runat="server" DataKeyNames="ID"
AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound"
OnRowUpdating="GridView1_RowUpdating"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing"
HeaderStyle-BackColor="CornflowerBlue" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White" CellPadding="5">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="" />
<asp:BoundField DataField="" />
<asp:BoundField DataField="" />
<asp:BoundField DataField="" />
<asp:BoundField DataField="" />
<asp:BoundField DataField="" />
<asp:BoundField DataField="" />
<asp:BoundField DataField="" />
<asp:BoundField DataField="" />
<asp:BoundField DataField="" />
<asp:BoundField DataField="" />
<asp:BoundField DataField="" />
<asp:BoundField DataField="" />
<asp:BoundField DataField="" />
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
代码代码:
code behind code :
private void Bind_CheckBoxList()
{
DataTable dtusers;
DataTable dtregions;
//String SQL = "SELECT P.ID ID, P.Name Name, P.Description Description, B.REGION_ID, B.REGION" +
// " FROM Product P, REGION_CONFIG B" +
// " WHERE P.BrandID=B.REGION_ID";
String SQLUsers = "select [USER_ID],USER_FIRST_NAME,USER_LAST_NAME from USER_INFO";
String SQLRegions = "select REGION_ID,REGION from REGION_CONFIG";
string sConstr = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
using (SqlConnection conn = new SqlConnection(sConstr))
{
using (SqlCommand comm = new SqlCommand(SQLUsers, conn))
{
conn.Open();
using (SqlDataAdapter dausers = new SqlDataAdapter(comm))
{
dtusers = new DataTable("tbl");
dausers.Fill(dtusers);
}
}
using (SqlCommand comm = new SqlCommand(SQLRegions, conn))
{
using (SqlDataAdapter daregions = new SqlDataAdapter(comm))
{
dtregions = new DataTable("tbl");
daregions.Fill(dtregions);
}
}
}
//DataTable dtAll;
//dtAll = dtusers.Copy();
//dtAll.Merge(dtregions);
GridView1.DataSource = dtusers;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataTable dt;
String SQL = "SELECT REGION FROM REGION_CONFIG";
string sConstr = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
using (SqlConnection conn = new SqlConnection(sConstr))
{
using (SqlCommand comm = new SqlCommand(SQL, conn))
{
conn.Open();
using (SqlDataAdapter da = new SqlDataAdapter(comm))
{
dt = new DataTable("tbl");
da.Fill(dt);
//DataRow row;
//row = dt.NewRow();
//dt.Rows.Add(row);
// dt.Columns.Add(new DataColumn("Nametest", typeof(string)));
}
}
}
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
BoundField boundField = new BoundField();
if (dt.Columns[i].ColumnName.ToString() != "Name")
{
boundField.DataField = dt.Columns[i].ColumnName.ToString();
boundField.HeaderText = dt.Columns[i].ColumnName.ToString();
GridView1.Columns.Add(boundField);
}
}
}
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
string strRegion;
strRegion = dt.Rows[i]["REGION"].ToString();
if (strRegion != "")
{
e.Row.Cells[i].Text = dt.Rows[i]["REGION"].ToString();
}
}
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
string strRegion;
strRegion = dt.Rows[i]["REGION"].ToString();
if (strRegion != "")
{
CheckBox cb = new CheckBox();
e.Row.Cells[i].Controls.Add(cb);
}
}
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
//CheckBoxList chkBrandedit = (CheckBoxList)e.Row.FindControl("CheckBoxList1");
//chkBrandedit.DataSource = dt;
//chkBrandedit.DataTextField = "Name";
//chkBrandedit.DataValueField = "ID";
//chkBrandedit.DataBind();
//chkBrandedit.SelectedValue = ((DataRowView)e.Row.DataItem)["BrandID"].ToString();
}
}
}
推荐答案
首先,您使用的是面向对象的编程语言。你需要让你的任务更轻松。
否则,你的代码将难以维护。
首先创建一个新类表示要在gridview中显示的数据。
根据自己的逻辑填充对象后。
您可以编写自己的gridview标记,也可以生成动态字段:
Hi,
First, you are using an Object Oriented Programming language. You need to make the task easier for you.
Otherwise, your code will be hard to maintain.
Start by creating a new class that represent the data to display in the gridview.
After you fill your objects based on your own logic.
You can either write your own gridview markup, or generate fields dynamicaly :
BoundField newColumnName= new BoundField();
newColumnName.DataField = "yourDataFieldName";
newColumnName.Headertext = "yourHeaderName";
yourGridView.Columns.Add(newColumnName);
我没有看到任何动态绑定思想的需要。
别忘了在gridview标记中添加
I dont see any need for a dynamic binding thought.
Dont forget to add
autogeneratedcolumns=false
这篇关于如何在gridview中绑定动态标头和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!