【问题标题】:gridview problem网格视图问题
【发布时间】:2010-10-12 07:13:22
【问题描述】:

我的 gridview 有一个问题,其中包含一个用于设置日期的文本框。

<asp:gridview ID="Gridview1" runat="server" AllowPaging="True" 
            AutoGenerateColumns="False" 
            DataSourceID="AccessDataSource1" CellPadding="8" ForeColor="#333333" 
            GridLines="None" CellSpacing="5" Height="361px" Width="748px">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:BoundField DataField="nome" HeaderText="Nome Utente" 
                SortExpression="nome" >
                <ItemStyle HorizontalAlign="Center" />
            </asp:BoundField>
            <asp:BoundField DataField="titolo" HeaderText="Titolo Libro" 
                SortExpression="titolo" >
                <ItemStyle HorizontalAlign="Center" /></asp:BoundField>
            <asp:BoundField DataField="Expr1" HeaderText="Data Restituzione Prevista" 
                ReadOnly="True" SortExpression="Expr1" >
                <ItemStyle HorizontalAlign="Center" /></asp:BoundField>
            <asp:TemplateField  
                HeaderText="Data Restituzione" SortExpression="Expr2">
                <EditItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("Expr2") %>'></asp:Label>
                </EditItemTemplate> 
                <ItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Expr2") %>' 
                        AutoPostBack="True" ontextchanged="TextBox1_TextChanged"></asp:TextBox>

                </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" />
            </asp:TemplateField>
        </Columns>

        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
        <SortedAscendingCellStyle BackColor="#FDF5AC" />
        <SortedAscendingHeaderStyle BackColor="#4D0000" />
        <SortedDescendingCellStyle BackColor="#FCF6C0" />
        <SortedDescendingHeaderStyle BackColor="#820000" />
        </asp:gridview>

这是我的 asp.cs 文件:

命名空间 Utenti_Biblio { 公共部分类 prestiti:System.Web.UI.Page { 布尔更改 = 假;

    protected void Page_Load(object sender, EventArgs e)
    {
        selectRow();
    }

    public void selectRow()
    {
        foreach (GridViewRow row in this.Gridview1.Rows)
        {
            TextBox textBox = (TextBox)row.Cells[3].FindControl("TextBox1");
            string a = textBox.Text;
            if (a != "")
            {
                row.Cells[3].FindControl("TextBox1").Visible = false;
            }
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        changed = true;
    }

    // salva
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (changed)
        {
            foreach (GridViewRow row in this.Gridview1.Rows)
            {
                TextBox textBox = (TextBox)row.Cells[3].FindControl("TextBox1");
                string qry = "UPDATE b_prestiti SET data_restituzione ='" + textBox.Text + "' WHERE id_utente = '" + row.Cells[1].ToString() + "'";
                OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                OleDbCommand cmd = conn.CreateCommand();
                OleDbDataReader reader = null;
                conn.Open();
                cmd.CommandText = qry;
                reader = cmd.ExecuteReader();
                ClientScript.RegisterStartupScript(this.GetType(), "conferma", "alert('Data Restituzione inserita!');window.location='Default.aspx';", true);
                reader.Close();

                conn.Close();
            }
        }

    }
}

}

当我在文本框中设置日期时如何更新数据库? 查询字符串不正确。谢谢!

【问题讨论】:

  • 请标记为适合您的答案!
  • 错误是“对象引用未设置为对象的实例。”
  • 错误在字符串qry中!

标签: c# asp.net gridview textbox


【解决方案1】:

使用 ExecuteNonQuery() 代替 ExecuteReader(),这可能会奏效。

【讨论】:

  • 错误是“对象引用未设置为对象的实例。”
  • 尝试使用这个而不是你使用的 TextBox textBox = (TextBox)row.Cells[3].FindControl(TextBox1.ClientID);但是控件 TextBox1 必须有一个 runat="server" 属性
  • 好的,但是错误在 where -> ("' WHERE id_utente = '" + row.Cells[1].ToString() + "'";)
  • id_utente 是 gridview 行中的第一个字段。
  • 那么应该是 ...Cells[0].Value.ToString()。 C# 是一种基于 0 的索引语言,这意味着每个计数器都从 0 开始(一行中的第一列的索引为 0,第二列的索引为 1,等等)
【解决方案2】:

GxG 是对的。 ExecuteReader() 用于获取单个记录(列),而 DML(数据操作语言,如 INSERT、DELETE、UPDATE)我们使用 ExecuteNonQuery()。

【讨论】:

  • 实际上 reader 带来了 Select 可能返回的所有行,然后您遍历 reader 以获取单个记录,列在 reader 的定义中。要获得单个值,您可以使用 ExecuteScalar() ,如果您有一个 out 参数,它只会带来过程的返回值。 :)
  • 是的,是的......再次正确:),谢谢你,我忘记了。
  • 如果您想访问网格中的第一个单元格,那么您应该在单元格数组中使用“0”,例如 ;row.Cells[0].ToString()
  • 只会返回类似“System.DataGrid.CellObject”之类的字符串,或类似的内容,以访问显示的文本,您需要使用 row.Cells[0].Text,或访问值 row.Cells[0].Value.ToString()
  • @GxG 这是常识 :),你不这么认为。
猜你喜欢
  • 1970-01-01
  • 2012-09-07
  • 2011-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多