473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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.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/xhtml" >

<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_SelectedIndexCha nged">

<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>



Jun 27 '08 #1
0 841

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Francis Lavoie | last post by:
Hello I have some questions regarding webframework, I must say that I quite lost and these questions are basicly to help me understand the way it work. I have some knowledge with PHP and JSP....
3
by: praba kar | last post by:
Dear All, I am new to Python. I am in need of some sorting functions (eg) numerical sorting functions and alphapetical sorting functions. I have searched through net But I cannot find any...
3
by: Samuel | last post by:
I wrote a very simple httpmodule and tried to compile it with no success. This is my code: ============== Imports System Imports System.Web Imports Microsoft.VisualBasic NameSpace...
7
by: Squignibbler | last post by:
Hi all, I have a question regarding the C++ programming language regarding the nature of the relationship between pointers and arrays. If the statement MyArray is functionally identical to...
8
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a bit of background: The company I work for is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.