473,406 Members | 2,387 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,406 software developers and data experts.

In ASP.NET DataGrid at Run Time dynamic columns are disappeared.

Friends
I am creating columns and edit button in ASP.NET DataGrid at Run Time but when I click on the Edit Button disappears all columns and edit button itself.
Only one column remains stay back, which I have defined in HTML Page.

Codes for Dynamically generating columns, I have written at Page_Load Event.

Please Give me your suggestions that where to write these code to generate Dynamic Columns at Run Time.

Thanks in Advance.
Aug 31 '07 #1
3 1535
ak1dnar
1,584 Expert 1GB
Create a separate sub for binding data to the datagrid. and don't call for it for each page loads. I think that might be the issue. Since your databinding is inside the page_load Once you click the Edit Button again page_load will display the default data grid, instead of Editable items.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.  If Not Page.IsPostBack Then
  3.             BindData()
  4.         End If
  5. End Sub
  6.  
  7. Public Sub BindData()
  8. 'bind the data to datagrid here
  9. End Sub
  10.  
  11.     Public Sub DataGrid_Edit(ByVal Source As Object, ByVal E As DataGridCommandEventArgs)
  12.         yourDataGrid.EditItemIndex = E.Item.ItemIndex
  13.         BindData()
  14.     End Sub
  15.  
Aug 31 '07 #2
Dear Friend,

I have been using following coding to generate dynamic columns in my Data Grid. Please have a look at, if you have few minutes free for the same.

Thanks a lot in advance.



<asp:datagrid id="DataGrid1" style="Z-INDEX: 114; LEFT: 112px; POSITION: absolute; TOP: 160px" runat="server" Width="344px" Height="148px" OnPageIndexChanged="DataGrid1_PageIndexChanged" AllowPaging="True" AutoGenerateColumns="False" PageSize="5" DataKeyField="SRNo">
<Columns>
<asp:TemplateColumn></asp:TemplateColumn>
<asp:BoundColumn DataField="SRNo" ReadOnly="True"
HeaderText="Roll No"></asp:BoundColumn>
</Columns>
</asp:datagrid>

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

If Not IsPostBack Then
DataBindGrid1()
End If

End Sub


Private Sub DataBindGrid1()
Dim Ds As New DataSet

Clm1.DataField = "pcat"
Clm1.HeaderText = "Theory Marks"
DataGrid1.Columns.Add(Clm1)

Clm2.DataField = "pcap"
Clm2.HeaderText = "Practical Marks"
DataGrid1.Columns.Add(Clm2)

Clm3.EditText = "Edit"
Clm3.UpdateText = "Update"
Clm3.CancelText = "Cancel"
Clm3.ButtonType = ButtonColumnType.PushButton
DataGrid1.Columns.Add(Clm3)


Dim Adp As New SqlDataAdapter("select srno, pcat, pcap from iyear",
Con)

Con.Open()

Adp.Fill(Ds, "iyear")
DataGrid1.DataSource = Ds
DataGrid1.DataMember = "iyear"

DataGrid1.DataBind()
Con.Close()
End Sub


Public Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEvent Args) Handles DataGrid1.PageIndexChanged

DataGrid1.CurrentPageIndex = e.NewPageIndex
DataBindGrid1()

End Sub


Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles DataGrid1.EditCommand

DataGrid1.EditItemIndex = e.Item.ItemIndex
DataBindGrid1()

End Sub
Sep 4 '07 #3
ak1dnar
1,584 Expert 1GB
Is there any specific error with this code snippet? or are you just wanted to show your coding. seems like its ok( for me). but there are experts here and they may say something, if they want to do so. I am not really good in ASP.net :)
Sep 4 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Ashish Shridharan | last post by:
Hi All I have been trying to add a control to the header cell of a datagrid on my ASP.NET page. These controls are defined in the HTML as ASP.NET web controls. They are being added into the...
0
by: lu | last post by:
I have a page with datagrid (for viewing, delete,edit,update) and text fields with button to add info to database. When user comes to page the datagrid selects info from database, binds in...
0
by: JP011 | last post by:
Hello I have hit a major road block when it comes to building my dynamic datagrid. To make a long story short I need a dynamic datagrid because my connection string could change and I need the...
6
by: Mark | last post by:
I have been working for quite some time on this issue which in theory should be quite simple. The problem is that the Cancel and Save events are not fired when their respective buttons are...
2
by: Ben | last post by:
Hi, I'd like to have a datagrid that has a dropdownlist in the pager control for setting the page size. I can get the control into the pager inside the datagrid itemcreated event by checking for...
0
by: optimizeit | last post by:
What I am attempting to do is import an Excel Workbook and display the worksheets in a datagrid dynamically. I am very close to getting this to work. I have to this point successfully imported a...
3
by: John E. | last post by:
I have a datatable that I am binding to a C# ASP.NET 1.1 web page datagrid. I also want to put an "Edit" column on the datagrid. However, whenever I use the following code, it puts the Edit...
7
by: CanoeGuy | last post by:
I have been trying for the last two weeks to display a dynamic DataGrid. The data that I'm pulling from a SQL Server DB will have whole columns that will be either NULL or 0. I want to display...
0
by: arunpandey | last post by:
Friends I am creating columns and edit button in ASP.NET DataGrid at Run Time but when I click on the Edit Button disappears all columns and edit button itself. Only one column remains stay back,...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.