[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Bhuwan Bhaskar

5/22/2008 3:46:00 PM

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.



Regards,
Bhuwan




using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

public partial class Update123 : System.Web.UI.Page

{



protected void Page_Load(object 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.DataSource = ds;

DataBind();



}



protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs 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.Item.Cells[1].Controls[0]).Text);

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

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

com.ExecuteNonQuery();

con.Close();

DataGrid1.EditItemIndex = -1;

databind();







}

protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)

{

DataGrid1.EditItemIndex = e.Item.ItemIndex;

databind();

}

protected void DataGrid1_SelectedIndexChanged(object sender, EventArgs e)

{



}

}



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

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

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

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

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

<div>

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

left: 193px; position: absolute; top: 80px" Width="275px" OnEditCommand="DataGrid1_EditCommand" OnUpdateCommand="DataGrid1_UpdateCommand" OnSelectedIndexChanged="DataGrid1_SelectedIndexChanged">

<Columns>

<asp:EditCommandColumn ButtonType="PushButton" CancelText="Cancel" EditText="Edit"

UpdateText="Update"></asp:EditCommandColumn>

<asp:BoundColumn HeaderText="name" DataField = "name" >

</asp:BoundColumn>

<asp:BoundColumn HeaderText="Addresssss" DataField = "address"></asp:BoundColumn>

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

</Columns>

</asp:DataGrid>

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


</div>

</form>

</body>

</html>