Regarding DataGrid_Update

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bhuwan Bhaskar

    Regarding DataGrid_Update


    Hi I am new for datagrid. I want to update the Datagrid. My code work fine. I want to hide the third column ie id (Which is also the primery key) from the user or make the column read only. I am using boundcolumn. I tried both way but unable to get the result. Making the column readonly, unable to get the value of id ,and update is unsuccessful. Hiding the column again have same result.



    Please suggest.



    Bhuwan





    using System;

    using System.Data;

    using System.Configur ation;

    using System.Collecti ons;

    using System.Web;

    using System.Web.Secu rity;

    using System.Web.UI;

    using System.Web.UI.W ebControls;

    using System.Web.UI.W ebControls.WebP arts;

    using System.Web.UI.H tmlControls;

    using System.Data.Sql Client;

    public partial class Update123 : System.Web.UI.P age

    {



    protected void Page_Load(objec t sender, EventArgs e)

    {



    if (!IsPostBack)

    {

    databind();

    }



    }

    public void databind()

    {

    SqlConnection con = new SqlConnection(" data source = (local); Database = online; Integrated Security =SSPI");

    SqlDataAdapter da = new SqlDataAdapter( "Select * from table1", con);

    DataSet ds = new DataSet();

    da.Fill(ds);



    DataGrid1.DataS ource = ds;

    DataBind();



    }



    protected void DataGrid1_Updat eCommand(object source, DataGridCommand EventArgs e)

    {



    SqlConnection con = new SqlConnection(" data source = (local); Database = online; Integrated Security =SSPI");

    string st = "Update table1 set name = @n , address = @ad where id = @id1";

    SqlCommand com = new SqlCommand(st, con);

    con.Open();

    com.Parameters. Add("@n", ((TextBox)e.Ite m.Cells[1].Controls[0]).Text);

    com.Parameters. Add("@ad", ((TextBox)e.Ite m.Cells[2].Controls[0]).Text);

    com.Parameters. Add("@id1", ((TextBox)e.Ite m.Cells[3].Controls[0]).Text);

    com.ExecuteNonQ uery();

    con.Close();

    DataGrid1.EditI temIndex = -1;

    databind();







    }

    protected void DataGrid1_EditC ommand(object source, DataGridCommand EventArgs e)

    {

    DataGrid1.EditI temIndex = e.Item.ItemInde x;

    databind();

    }

    protected void DataGrid1_Selec tedIndexChanged (object sender, EventArgs e)

    {



    }

    }







    <%@ Page Language="C#" AutoEventWireup ="true" CodeFile="Updat e123.aspx.cs" Inherits="Updat e123" %>



    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">



    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head runat="server">

    <title>Untitl ed Page</title>

    </head>

    <body>

    <form id="form1" runat="server">

    <div>

    <asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateCol umns="False" Style="z-index: 100;

    left: 193px; position: absolute; top: 80px" Width="275px" OnEditCommand=" DataGrid1_EditC ommand" OnUpdateCommand ="DataGrid1_Upd ateCommand" OnSelectedIndex Changed="DataGr id1_SelectedInd exChanged">

    <Columns>

    <asp:EditComman dColumn ButtonType="Pus hButton" CancelText="Can cel" EditText="Edit"

    UpdateText="Upd ate"></asp:EditCommand Column>

    <asp:BoundColum n HeaderText="nam e" DataField = "name"

    </asp:BoundColumn >

    <asp:BoundColum n HeaderText="Add resssss" DataField = "address"></asp:BoundColumn >

    <asp:BoundColum n HeaderText="Id" DataField = "id" ></asp:BoundColumn >

    </Columns>

    </asp:DataGrid>

    <span style="text-decoration: underline"></span>



    </div>

    </form>

    </body>

    </html>



Working...