很简单的问题,在线等!gridview外的按钮怎么取不到gridview内的数据啊!
编了个很简单的程序,其中有个gridview,外面有个save按钮,点击按钮,获取gridview的数据,保存到数据库。可是,每次取出gridview.columns.count总是0,里面的数据也是空,那位牛人,帮忙看一下啊<form id="form1" runat="server">
<div>
<asp:Panel ID="Panel1" runat="server" GroupingText="自定义报表设置" Height="209px" Width="917px">
<table style="width: 909px">
<tr>
<td style="width: 280px; height: 192px;" valign="top">
<asp:Label ID="Label1" runat="server" Text="数据表:"></asp:Label>
<asp:DropDownList ID="ddlTable" runat="server" Width="201px" AutoPostBack="True" OnSelectedIndexChanged="ddlTable_SelectedIndexChanged">
</asp:DropDownList>
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="请选择要显示的字段名:"></asp:Label>
<asp:CheckBox ID="chkAll" runat="server" Text="全选" AutoPostBack="True" OnCheckedChanged="chkAll_CheckedChanged1" />
<asp:Panel ID="Panel2" runat="server" Height="123px" ScrollBars="Auto" Width="268px" BorderStyle="Solid" BorderWidth="1px">
<asp:CheckBoxList ID="chkField" runat="server" Width="241px" OnSelectedIndexChanged="chkField_SelectedIndexChanged" AutoPostBack="True">
</asp:CheckBoxList><br />
<br />
</asp:Panel>
</td>
<td style="width: 540px; height: 192px;" valign="top">
<asp:Panel ID="Panel3" runat="server" Height="177px" Width="628px" GroupingText="查询条件设置">
<asp:Label ID="Label3" runat="server" Text="字段名称:"></asp:Label><asp:DropDownList ID="ddlField" runat="server" Width="152px">
</asp:DropDownList>
<asp:Label ID="Label4" runat="server" Text="运算关系:"></asp:Label><asp:DropDownList
ID="ddlCaculate" runat="server" Width="84px">
<asp:ListItem Value="like">包含</asp:ListItem>
<asp:ListItem Value="=">等于</asp:ListItem>
<asp:ListItem Value=">">大于</asp:ListItem>
<asp:ListItem Value=">=">大于等于</asp:ListItem>
<asp:ListItem Value="<">小于</asp:ListItem>
<asp:ListItem Value="<=">小于等于</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="Label5" runat="server" Text="字 段 值:"></asp:Label><asp:TextBox
ID="txtField" runat="server" Width="112px"></asp:TextBox><asp:Label ID="Label6" runat="server"
Text="逻辑关系:"></asp:Label><asp:DropDownList ID="ddlLogic" runat="server" Width="152px">
<asp:ListItem Value="and">与</asp:ListItem>
<asp:ListItem Value="or">或</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btnAdd" runat="server" Text="添加" OnClick="btnAdd_Click" />
<asp:Button ID="btnClear" runat="server" Text="清空" OnClick="btnClear_Click" />
<asp:Button ID="btnSearch" runat="server" Text="查询" OnClick="btnSearch_Click" />
<asp:Button ID="addrec" runat="server" OnClick="addrec_Click" Text="插入一条新记录" Width="117px" /><br />
<asp:TextBox ID="txtSearch" runat="server" Height="127px" Width="606px" TextMode="MultiLine" Enabled="False"></asp:TextBox></asp:Panel>
</td>
</tr>
</table>
<asp:HiddenField ID="hidWhereSql" runat="server" />
<asp:HiddenField ID="hidSelectSql" runat="server" /><asp:HiddenField ID="hidsqlyj" runat="server" />
</asp:Panel>
<asp:Label ID="Label7" runat="server"></asp:Label> <br />
<asp:Panel runat="server">
<asp:GridView ID="myGv" runat="server" OnRowDeleting="myGv_RowDeleting" OnRowEditing="myGv_RowEditing" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" BorderWidth="2px" Width="910px">
<RowStyle BackColor="WhiteSmoke" HorizontalAlign="Center" />
<PagerStyle BackColor="White" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="Maroon" Font-Bold="True" ForeColor="WhiteSmoke" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="#E0E0E0" />
</asp:GridView>
<div>
<table width="900" border="0">
<tr><td style="width: 855px">
<asp:GridView ID="myGv2" runat="server" OnRowCommand="btnClick" OnRowDataBound="myGv2_RowDataBound" BorderWidth="2px" Width="910px">
<RowStyle BackColor="White" HorizontalAlign="Center" VerticalAlign="Middle" />
<PagerStyle BackColor="White" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="Maroon" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="#E0E0E0" />
<Columns>
<asp:ButtonField CommandName="save_btn" Text="保存" ButtonType="Button" />
<asp:ButtonField ButtonType="Button" CommandName="cancel_btn" Text="返回" />
</Columns>
</asp:GridView></td>
</tr>
</table>
</div>
</asp:Panel>
</div>
</form>
下面是c#程序
public void myGv_RowEditing(object sender, GridViewEditEventArgs e)
{
myGvunvis();
czbs = 2;
string sqlstr = "select * from " + ddlTable.SelectedItem.Value.ToString() + " where id='" + myGv.DataKeys[e.NewEditIndex].Value.ToString()+ "'";
Database myDb = new Database();
DataSet myDs = myDb.GetDataSet(sqlstr);
myGv2.DataKeyNames = new string[] { "id" };
myGv2.DataSource = myDs;
myGv2.DataBind();
this.Form.Controls.Add(myGv2);
fblsql = "update wbin." + ddlTable.SelectedItem.Value.ToString() + " set ";
hm = " where id='" + myGv.DataKeys[e.NewEditIndex].Value.ToString() + "'";
}
public void myGvunvis()
{
myGv.Visible = false;
myGv2.Visible = true;
}
public void myGvvis()
{
myGv.Visible = true;
myGv2.Visible = false;
}
protected void myGv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sqlstr = "delete from wbin." + ddlTable.SelectedItem.Value.ToString() +" where id='" + myGv.DataKeys[e.RowIndex].Value.ToString() + "'";
OracleConnection sqlcon = sss.getConnection();
OracleCommand sqlcom = new OracleCommand(sqlstr, sqlcon);
if (sqlcon.State == ConnectionState.Open)
{ sqlcon.Close(); }
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
myGvBind();
}
public void myGvBind()
{
String ba = hidsqlyj.Value;
OracleConnection myConn = sss.getConnection();
//定义SQL语句
//实例化SqlDataAdapter对象
OracleDataAdapter da = new OracleDataAdapter(ba, myConn);
//实例化数据集DataSet
DataSet ds = new DataSet();
da.Fill(ds);
//绑定DataList控件
myGv.DataSource = ds;//设置数据源,用于填充控件中的项的值列表
myGv.DataKeyNames = new string[] { "id" };
myGv.RowDataBound += new GridViewRowEventHandler(this.On_RowDataBound);
myGv.DataBind();//将控件及其所有子控件绑定到指定的数据源
}
protected void qr_Click(object sender, EventArgs e)
{
if (czbs == 2)
{
//myGv2.Visible = true;
//this.Label7.Text=this.myGv2.Rows[0].Cells[0].Text;
//string f5 = "";
//for (int i = 0; i < myGv2.HeaderRow.Cells.Count; i++)
//{
// f5 = f5 + myGv2.HeaderRow.Cells[i].Text + "='" + myGv2.Rows[0].Cells[i].Text.Trim() + "',";
//}
//fblsql = fblsql + f5 + hm;
//this.Label7.Text = fblsql;
//=myGv2.HeaderRow.Cells.Count.ToString();
//myGvvis();
}
else if (czbs == 1)
{
this.Label7.Text = "插入";
myGvvis();
}
else { this.Label7.Text = czbs.ToString(); }
}
就是上面这个按钮的事件,本来有很多操作,可是死活取不出来(gridview)myGv2的数据,总是显示为0,太郁闷了,谢谢大家了,帮帮忙。 --------------------编程问答-------------------- 1、是否连接同一个数据库;
2、不操作数据库的时候,具体看看DATAGRIDVIEW有数据? --------------------编程问答-------------------- 太长了,看的头疼
补充:.NET技术 , C#