locked
A problem with a GridView in a aspx page in ASP with C# RRS feed

  • 問題

  •  

    Hi everyone!

     

    I'm Florin G and I have a problem with a GridView. So my problem is:

     

    I have made a GridView in a aspx form (designer). This control is populated with a SqlDataSource.

    My table in Sql is TipContinut with 2 columns Nr_poz, Tip_continut. Nr_poz - is primary key.

     

    My code in aspx (html?) is:

    <td colspan="2" style="width: 770px; height: 400px;">

    <asp:GridView ID="grdViewTipContinut" runat="server"

    Style="position: relative; left: 245px; top: -63px;" AutoGenerateColumns="False" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None" DataSourceID="SqlDataSource_grdEditTipC" OnRowDeleting="grdViewTipContinut_RowDeleting">

    <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />

    <Columns>

    <asp:BoundField DataField="Nr_poz" HeaderText="Nr pozitie" HtmlEncode="False" ReadOnly="True" />

    <asp:BoundField DataField="Tip_Continut" HeaderText="Tip continut" HtmlEncode="False" />

    <asp:CommandField ShowEditButton="True" />

    <asp:CommandField ShowDeleteButton="True" />

    </Columns>

    <RowStyle BackColor="#DEDFDE" ForeColor="Black" />

    <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />

    <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />

    <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />

    </asp:GridView>

     

    <asp:SqlDataSource ID="SqlDataSource_grdEditTipC" runat="server"

    UpdateCommand="UPDATE [TipContinut] SET [Tip_continut] = UPPER(@Tip_continut) WHERE [Nr_poz] = @Nr_poz">

    <UpdateParameters>

    <asp:Parameter Name="Tip_continut" Type="String" />

    <asp:Parameter Name="Nr_poz" Type="Int32" />

    </UpdateParameters>

    </asp:SqlDataSource>

    </td>

     

    And my code in C# is:

    protected void Page_Load(object sender, EventArgs e)

    /*---------------------------------------------------------------------------*/

    {

    //for select

    SqlDataSource_grdEditTipC.ConnectionString = String.Format("Data Source={0}; Initial Catalog={1}; Persist

    Security Info=True; User ID={2}; Password={3}",

    Request.Cookies["strServerName"].Value, Request.Cookies["strNameDatabase"].Value,

    Request.Cookies["strUIDSaName"].Value, Request.Cookies["strSaPWD"].Value);

    SqlDataSource_grdEditTipC.SelectCommand = "SELECT Nr_poz, Tip_continut FROM TipContinut";

    }//end protected void Page_Load(object sender, EventArgs e)

     

    protected void btnInserareTipContinut_Click(object sender, EventArgs e)

    /*---------------------------------------------------------------------*/

    {

    //for insert

    SqlDataSource_grdEditTipC.InsertCommand = String.Format("INSERT INTO TipContinut " +

    "SELECT ISNULL(MAX(Nr_poz), 0) + 1 AS Nr_poz, '{0}' AS Tip_continut " +

    "FROM TipContinut",

    txtBoxEditTipContinut.Text.Trim().ToUpper());

    SqlDataSource_grdEditTipC.Insert();

    }//end protected void btnInserareTipContinut_Click(object sender, EventArgs e)

     

     

    protected void grdViewTipContinut_RowDeleting(object sender, GridViewDeleteEventArgs e)

    /*-------------------------------------------------------------------------------------*/

    {

    //for delete

    SqlDataSource_grdEditTipC.DeleteCommand = String.Format("DELETE FROM TipContinut WHERE (Tip_Continut

    = '{0}') AND (Nr_poz={1})",

    grdViewTipContinut.Rows[e.RowIndex].Cells[1].Text.Trim(),

    grdViewTipContinut.Rows[e.RowIndex].Cells[0].Text.Trim());

    SqlDataSource_grdEditTipC.Delete();

     

    }//end protected void grdViewTipContinut_RowDeleting(object sender, GridViewDeleteEventArgs e)

     

     

    The colomn Nr_poz is ReadOnly = true;

     

    So my problem is: the delete command is working ok, the insert command is working ok, the select command is working ok BUT the UPDATE command isn't working because my column Nr-poz is ReadOnly. But I want that this column to be readonly.

     

    So How to do this thing? My table has only 2 column Nr_poz and Tip_continut.

     

    In my GridView I show to my clients Nr_poz and Tip_continut, but I want that my clients to update only the column Tip_continut so I put Nr-poz to readonly.

     

    Please help me!

    What's wrong?

    What is the correct answer?

     

     

    Thanks,

     

     

     

     

     

     

     

     

     

     

     

    2008年10月7日 下午 01:50

解答

  • Hi Florin G

    Have you try to put a ConnectionString Property on your SQL Data Source for the Update Command?

    E.g.

    <asp:SqlDataSource ID="SqlDataSource_grdEditTipC" runat="server" ConnectionString="<%$ ConnectionStringVariable %>"

    UpdateCommand="UPDATE [TipContinut] SET [Tip_continut] = UPPER(@Tip_continut) WHERE [Nr_poz] = @Nr_poz">

    <UpdateParameters>

    <asp:Parameter Name="Tip_continut" Type="String" />

    <asp:Parameter Name="Nr_poz" Type="Int32" />

    </UpdateParameters>

    </asp:SqlDataSource>


    Also I think you need to set DataKeyName property on your Grid View

    E.g.

    <asp:GridView ID="grdViewTipContinut" runat="server"

    Style="position: relative; left: 245px; top: -63px;" AutoGenerateColumns="False" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None" DataSourceID="SqlDataSource_grdEditTipC" OnRowDeleting="grdViewTipContinut_RowDeleting"

    DataKeyName="Nr_poz"

    >

    I am not 100% sure, if it would work, but it is no harm to try. (*I seldom use SQLDataSource)


    Hope it would work.

    2008年10月7日 下午 03:29
  • Not with ConnectionString but it works with

    DataKeyNames="Nr_poz"

     

    So in my code I added

    DataKeyNames="Nr_poz" and it worked very well.

     

     

    Thank You very much!

     

    Have a nice day!

    2008年10月8日 上午 08:59

所有回覆

  • Hi Florin G

    Have you try to put a ConnectionString Property on your SQL Data Source for the Update Command?

    E.g.

    <asp:SqlDataSource ID="SqlDataSource_grdEditTipC" runat="server" ConnectionString="<%$ ConnectionStringVariable %>"

    UpdateCommand="UPDATE [TipContinut] SET [Tip_continut] = UPPER(@Tip_continut) WHERE [Nr_poz] = @Nr_poz">

    <UpdateParameters>

    <asp:Parameter Name="Tip_continut" Type="String" />

    <asp:Parameter Name="Nr_poz" Type="Int32" />

    </UpdateParameters>

    </asp:SqlDataSource>


    Also I think you need to set DataKeyName property on your Grid View

    E.g.

    <asp:GridView ID="grdViewTipContinut" runat="server"

    Style="position: relative; left: 245px; top: -63px;" AutoGenerateColumns="False" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None" DataSourceID="SqlDataSource_grdEditTipC" OnRowDeleting="grdViewTipContinut_RowDeleting"

    DataKeyName="Nr_poz"

    >

    I am not 100% sure, if it would work, but it is no harm to try. (*I seldom use SQLDataSource)


    Hope it would work.

    2008年10月7日 下午 03:29
  • Not with ConnectionString but it works with

    DataKeyNames="Nr_poz"

     

    So in my code I added

    DataKeyNames="Nr_poz" and it worked very well.

     

     

    Thank You very much!

     

    Have a nice day!

    2008年10月8日 上午 08:59