GridView中如何加入DropDownList和CheckBox等控件的問題
GridView的設計是6列:
第一列為行號;
第二列為checkbox,用來表示選中當前行(可同時選擇多行);
第三至第六列爲從數據庫讀取的數據:
第三列:只讀的TextBox;
第四列:可編輯的TextBox;
第五列:DropDownList(datasource另讀);
第六列:只讀的TextBox。為數據庫中的2列數據拼合(yyyy/mm/dd hh:mm),在服務器端代碼中用了string的拼接,如果有更好的方法請賜教;
需求是進入畫面后,Grid中的非只讀控件都可直接操作
以下是客戶端的代碼
1、嘗試加了checkbox,可以顯示並購選,但還不清楚獲取選中狀態的用法;
2、嘗試加了DropDownList,綁定數據之前只能顯示成TextBox的樣子。而在服務器端綁定數據后,程序報錯
<asp:GridView ID="GridView1" runat="server" Font-Size="Medium" BackColor="Gray"
CellPadding="1" AllowSorting="True"
onrowcreated="GridView1_RowDataBound" >
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="選択">
<ItemTemplate>
<input ID="Checkbox1" type="checkbox" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="端末№">
<ItemTemplate>
<asp:DropDownList id="dlTerNo" runat="Server" autopostback="true" >
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
public partial class Page1 : System.Web.UI.Page
{
int m_nDataRow = 0;
CTblDUADDINF m_dbData = new CTblDUADDINF();
STCDUADDINF[] m_aDuaddinf = null;
struct STCDUADDINF
{
public string strMacAddress;
public string strCompanyCode;
public int nTerminalNo;
public int nAccessYMD;
public short nAccessHM;
};
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = GetDataSource();
GridView1.DataBind();
}
}
protected bool ReadDUADDINF()// 打開表
{
string strSql = string.Format("select {0}, {1}, {2}, {3}, {4} From {5}", CTblDUADDINF.S_MacAddress, CTblDUADDINF.S_CompanyCode, CTblDUADDINF.S_TerminalNo, CTblDUADDINF.S_AccessYMD, CTblDUADDINF.S_AccessHM, CTblDUADDINF.S_Tbl_Name);
m_dbData.Open(strSql, out m_nDataRow);
if (m_nDataRow > 0)
{
return true;
}
return false;
}
private DataTable GetDataSource()// 獲取數據
{
int nCnt = 0;
DataTable dt = new DataTable();
if (!IsPostBack)
{
if (ReadDUADDINF())
{
m_aDuaddinf = null;
m_aDuaddinf = new STCDUADDINF[m_nDataRow];
for (nCnt = 0; nCnt < m_nDataRow; nCnt++)
{
m_aDuaddinf[nCnt].strMacAddress = m_dbData.m_data.Tables[0].Rows[nCnt]["MacAddress"].ToString().Trim();
m_aDuaddinf[nCnt].strCompanyCode = m_dbData.m_data.Tables[0].Rows[nCnt]["CompanyCode"].ToString();
m_aDuaddinf[nCnt].nTerminalNo = (short)m_dbData.m_data.Tables[0].Rows[nCnt]["TerminalNo"];
m_aDuaddinf[nCnt].nAccessYMD = (int)m_dbData.m_data.Tables[0].Rows[nCnt]["AccessYMD"];
m_aDuaddinf[nCnt].nAccessHM = (short)m_dbData.m_data.Tables[0].Rows[nCnt]["AccessHM"];
}
}
//dt.Columns.Add(" ", typeof(string));
//dt.Columns.Add("選択", typeof(bool));
dt.Columns.Add("会社コード", typeof(string));
dt.Columns.Add("端末ノードアドレス", typeof(string));
dt.Columns.Add("端末№", typeof(int));
dt.Columns.Add("最新アクセス時間", typeof(string));
for (nCnt = 0; nCnt < m_nDataRow; nCnt++)
{
DataRow dr = dt.NewRow();
//dr[" "] = nCnt.ToString();
//dr["選択"] = 1;
dr["会社コード"] = m_aDuaddinf[nCnt].strCompanyCode;
dr["端末ノードアドレス"] = m_aDuaddinf[nCnt].strMacAddress;
dr["端末№"] = m_aDuaddinf[nCnt].nTerminalNo.ToString();
string strTemp1 = m_aDuaddinf[nCnt].nAccessYMD.ToString();
strTemp1 = strTemp1.Substring(0, 4) + "/" + strTemp1.Substring(4, 2) + "/" + strTemp1.Substring(6);
string strTemp2 = m_aDuaddinf[nCnt].nAccessHM.ToString();
strTemp2 = strTemp2.Substring(0, 2) + ":" + strTemp2.Substring(2);
dr["最新アクセス時間"] = strTemp1 + " " + strTemp2;
dt.Rows.Add(dr);
}
}
return dt;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
// 第一列顯示行號
if (e.Row.RowIndex != -1)
{
int id = e.Row.RowIndex + 1;
e.Row.Cells[0].Text = id.ToString();
}
// 第五列用DropDownList顯示,失敗。。
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((DropDownList)e.Row.FindControl("dlTerNo") != null)
{
DropDownList dlTerNo = (DropDownList)e.Row.FindControl("dlTerNo");
dlTerNo.DataSource = GetDropData();
dlTerNo.DataTextField = "name";
dlTerNo.DataValueField = "id";
dlTerNo.DataBind();
}
}
}
private DataTable GetDropData()// DropDownList獲取數據
{
DataTable dt = new DataTable();
dt.Columns.Add("1", typeof(int));
for (int nCnt = 0; nCnt < 3; nCnt++)
{
DataRow dr = dt.NewRow();
dr["1"] = nCnt;
dt.Rows.Add(dr);
}
return dt;
}
}
<asp:DropDownList ID="Ddl_TermNo" runat="Server" DataSource='<%# GetDropData()%>'
DataValueField="端末№" DataTextField="端末№">
private DataTable GetDropData()
{
DataTable dt = new DataTable();
dt.Columns.Add("1", typeof(int));
for (int nCnt = 0; nCnt < 3; nCnt++)
{
DataRow dr = dt.NewRow();
dr["1"] = nCnt;
dt.Rows.Add(dr);
}
return dt;
}
<asp:GridView ID="GridView1" runat="server" Font-Size="9pt" BackColor="White"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="3"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
>
<Columns>
<asp:TemplateField HeaderText="">
</asp:TemplateField>
<asp:TemplateField HeaderText="選択">
<ItemTemplate>
<input ID="Checkbox1" type="checkbox" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CompanyCode" HeaderText="会社コード" SortExpression="会社コード" />
<asp:BoundField DataField="MacAddress" HeaderText="端末ノードアドレス" SortExpression="端末ノードアドレス" />
<asp:TemplateField HeaderText="端末№">
<ItemTemplate>
<asp:DropDownList ID="Ddl_TermNo" runat="Server" DataSource='<%# GetDropData()%>'
DataValueField="TerminalNo" DataTextField="TerminalNo">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="AccessYMD" HeaderText="最新アクセス時間" SortExpression="最新アクセス時間" />
</Columns>
<RowStyle ForeColor="#000066" />
</asp:GridView>
public partial class Page1 : System.Web.UI.Page
{
int m_nDataRow = 0;
CTblDUADDINF m_dbData = new CTblDUADDINF();//自定义的表对象
protected void Page_Load(object sender, EventArgs e)
{
DropDownList ddl;
if (!IsPostBack)
{
DataSet ds = new DataSet();
ds = GetDataSource();//从数据库中读出的dataset
GridView1.DataSource = ds;
GridView1.DataBind();// 程序跑到这里崩了,错误见下图
// 设置dropdownlist当前选中的项
for (m_nDataRow = 0; m_nDataRow < GridView1.Rows.Count - 1; m_nDataRow++)
{
DataRowView drv = ds.Tables[0].DefaultView[m_nDataRow];
if (Convert.ToString(drv["TerminalNo"]).Trim() == "0")
{
ddl = (DropDownList)GridView1.Rows[m_nDataRow].FindControl("Ddl_TermNo");
ddl.SelectedIndex = 0;
}
else if (Convert.ToString(drv["TerminalNo"]).Trim() == "1")
{
ddl = (DropDownList)GridView1.Rows[m_nDataRow].FindControl("Ddl_TermNo");
ddl.SelectedIndex = 1;
}
else if (Convert.ToString(drv["TerminalNo"]).Trim() == "2")
{
ddl = (DropDownList)GridView1.Rows[m_nDataRow].FindControl("Ddl_TermNo");
ddl.SelectedIndex = 2;
}
}
}
}
protected bool ReadDUADDINF()// 读表
{
string strSql = string.Format("select {0}, {1}, {2}, {3} From {4}", CTblDUADDINF.S_CompanyCode, CTblDUADDINF.S_MacAddress, CTblDUADDINF.S_TerminalNo, CTblDUADDINF.S_AccessYMD, CTblDUADDINF.S_Tbl_Name);
m_dbData.Open(strSql, out m_nDataRow);
if (m_nDataRow > 0)
{
return true;
}
return false;
}
protected DataSet GetDataSource()
{
int nCnt = 0;
DataTable dt = new DataTable();
if (!IsPostBack)
{
if (ReadDUADDINF())
{
return m_dbData.m_data;// 自定义的表对象,返回的是dataset类型
}
DataSet ds = new DataSet();
return ds;//这里应该不会跑到,随便写的
}
protected DataSet GetDropData()// dropdownlist中的候选项
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add("1", typeof(int));
for (int nCnt = 0; nCnt < 3; nCnt++)
{
DataRow dr = dt.NewRow();
dr["1"] = nCnt;
dt.Rows.Add(dr);
}
ds.Tables.Add(dt);
return ds;
}
DataTable dt = new DataTable();
dt.Columns.Add("1", typeof(int));
for (int nCnt = 0; nCnt < 3; nCnt++)
{
DataRow dr = dt.NewRow();
dr["1"] = nCnt;
dt.Rows.Add(dr);
}
return dt;
}
-----------------------
你的数据只有 0、1、2吗?直接写到前台就好了
[其他解释]
mark
[其他解释]
dt.Columns.Add("TerminalNo", typeof(int));
for (int nCnt = 0; nCnt < 3; nCnt++)
{
DataRow dr = dt.NewRow();
dr["TerminalNo"] = nCnt;
dt.Rows.Add(dr);
}
return dt;
}
[其他解释]