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

Where to write code for button event handler in a GridView

hello all...I have a girdview(with two columns)...the first column has buttons and second column is empty......so my task is everytime I click on the button the corresponding row(2nd column) should be filled with the current datetime...can anyone tell me how to do this....i wil be thankful to anykind of help...
Sep 19 '08 #1
8 8552
i am giving sample code impliment it in your own way

add this in aspx

Expand|Select|Wrap|Line Numbers
  1. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="111px" >
  2.             <Columns>
  3.                 <asp:ButtonField ButtonType="Button" Text="Button" />
  4.                 <asp:BoundField HeaderText="Time" DataField="Time" />
  5.  
  6.             </Columns>
  7.         </asp:GridView>
  8.  
  9. add this in aspx.cs page
  10.  
  11.  protected void Page_Load(object sender, EventArgs e)
  12.     {
  13.         DataTable dt = new DataTable();
  14.         dt.Columns.Add("Time");
  15.  
  16.         DataRow dr = dt.NewRow();
  17.  
  18.         dr["Time"] = DateTime.Now.ToString();
  19.  
  20.         dt.Rows.Add(dr);
  21.  
  22.         GridView1.DataSource = dt;
  23.         GridView1.DataBind();
  24.  
  25.     }
Sep 19 '08 #2
hello..thks for ur reply...u dint my question completely.....my gridview has 10rows.....with 10buttons in all the rows....now I need the event handler where I can write the code to display the datetime whenever I click the button(in the 2nd column in the same row).....for example....if i click the first row button..then it should display datetime in the second column of the same row......then if I click on the second row button then datetime should be displayed in the second column of the same row..and so on......



i am giving sample code impliment it in your own way

add this in aspx

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="111px" >
<Columns>
<asp:ButtonField ButtonType="Button" Text="Button" />
<asp:BoundField HeaderText="Time" DataField="Time" />

</Columns>
</asp:GridView>

add this in aspx.cs page

protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Time");

DataRow dr = dt.NewRow();

dr["Time"] = DateTime.Now.ToString();

dt.Rows.Add(dr);

GridView1.DataSource = dt;
GridView1.DataBind();

}
Sep 19 '08 #3
Tyr with this

in aspx

Expand|Select|Wrap|Line Numbers
  1. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="111px"
  2.          OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
  3.             <Columns>
  4.                 <asp:CommandField ButtonType="Button" ShowSelectButton="True" />
  5.                 <asp:BoundField HeaderText="Time" DataField="Time" />              
  6.             </Columns>
  7.         </asp:GridView>
in aspx.cs page

Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e)
  2.     {
  3.         if (!IsPostBack)
  4.         {
  5. //Actual you need to write your main logic here to get data into table
  6. // if you are collecting data through query
  7. // write in query like select *,getdate() as Time form tablename .....
  8. //so will get the column name extra as "Time"
  9.  
  10.             DataTable dt = new DataTable();
  11.             dt.Columns.Add("Time");
  12.  
  13.             DataRow dr = dt.NewRow();
  14.             dr["Time"] = DateTime.Now.ToString();
  15.             dt.Rows.Add(dr);
  16.  
  17.             DataRow dr1 = dt.NewRow();
  18.             dr1["Time"] = DateTime.Now.ToString();
  19.             dt.Rows.Add(dr1);
  20.  
  21.             DataRow dr2 = dt.NewRow();
  22.             dr2["Time"] = DateTime.Now.ToString();
  23.             dt.Rows.Add(dr2);
  24.  
  25.             GridView1.DataSource = dt;
  26.             GridView1.DataBind();
  27.  
  28.             ViewState["table"] = dt;
  29.         }
  30.  
  31.     }
  32.     protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
  33.     {
  34.         DataTable dtGV = (DataTable)ViewState["table"];
  35.  
  36.         dtGV.Rows[GridView1.SelectedIndex]["Time"] = DateTime.Now.ToString();
  37.  
  38.         dtGV.Rows[GridView1.SelectedIndex].AcceptChanges();
  39.  
  40.         GridView1.DataSource = dtGV;
  41.         GridView1.DataBind();
  42.  
  43.         ViewState["table"] = dtGV;
  44.     }
Sep 19 '08 #4
i dont understand y u always go for a datatable.....imagine i got 10rows after I bind the datasource to a gridview....now I want the system datetime to be taken in to the corresponding cell(same row and 2nd column) and I hope thr's nothing to do with the datatable...






Tyr with this

in aspx

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="111px"
OnSelectedIndexChanged="GridView1_SelectedIndexCha nged">
<Columns>
<asp:CommandField ButtonType="Button" ShowSelectButton="True" />
<asp:BoundField HeaderText="Time" DataField="Time" />
</Columns>
</asp:GridView>


in aspx.cs page


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Actual you need to write your main logic here to get data into table
// if you are collecting data through query
// write in query like select *,getdate() as Time form tablename .....
//so will get the column name extra as "Time"

DataTable dt = new DataTable();
dt.Columns.Add("Time");

DataRow dr = dt.NewRow();
dr["Time"] = DateTime.Now.ToString();
dt.Rows.Add(dr);

DataRow dr1 = dt.NewRow();
dr1["Time"] = DateTime.Now.ToString();
dt.Rows.Add(dr1);

DataRow dr2 = dt.NewRow();
dr2["Time"] = DateTime.Now.ToString();
dt.Rows.Add(dr2);

GridView1.DataSource = dt;
GridView1.DataBind();

ViewState["table"] = dt;
}

}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dtGV = (DataTable)ViewState["table"];

dtGV.Rows[GridView1.SelectedIndex]["Time"] = DateTime.Now.ToString();

dtGV.Rows[GridView1.SelectedIndex].AcceptChanges();

GridView1.DataSource = dtGV;
GridView1.DataBind();

ViewState["table"] = dtGV;
}
Sep 19 '08 #5
To show modifications/updations in the grid normally we will update in the table and bind that table to grid. This is easy insted of doing on grid. why bcoz if we bind our grid with table/dataset once again the modifications which are doing on grid will be lost. Grid will freshly rebind the rows and columns which are in table/dataset. if we are doing on grid directly we need find the exact control and need to update that one.
Here i m giving like this example may be it will match with
Sep 20 '08 #6
Defaultly added............................................
Sep 20 '08 #7
To show modifications/updations in the grid normally we will update in the table and then bind that table to grid. This is easy insted of doing on grid. why bcoz if we bind our grid with table/dataset once again the modifications which are doing on grid will be lost. Grid will freshly rebind the rows and columns which are in table/dataset. if we are doing on grid directly we need to find the exact control and need to update that one.
I dont know in which process you r doing. So i have given like that.i think its better to add item template with lable control to your grid as bellow

Expand|Select|Wrap|Line Numbers
  1.  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="111px"
  2. OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
  3. <Columns>
  4. <asp:CommandField ButtonType="Button" ShowSelectButton="True" />
  5.  
  6.    <asp:TemplateField HeaderText="time label">
  7.         <ItemTemplate>
  8.             <asp:Label ID="lbltime" runat="server" />
  9.         </ItemTemplate>    
  10.     </asp:TemplateField>
on selected index change of grid find that control and update that control as bellow . Dont bind grid again with table/dataset. Bcoz it will lose the data.

Expand|Select|Wrap|Line Numbers
  1. protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
  2.     {
  3.         Label lblTime = ((Label)GridView1.Rows[GridView1.SelectedIndex].Cells[1].FindControl("lbltime"));
  4. //Here Cells[1] is 2nd column "time label" 
  5.         lblTime.Text = DateTime.Now.ToString();
  6. }
Sep 20 '08 #8
hi..thks for ur reply....this is working fine....if i click on a button..i can see the datetime in the "time label" column(in the same row as that of the button clicked)...but the problem with this is....if i click on the other button(s), the previous datetime is getting cleared...i want all the datetimes to be there until the .aspx page is closed....plz reply soon..


To show modifications/updations in the grid normally we will update in the table and then bind that table to grid. This is easy insted of doing on grid. why bcoz if we bind our grid with table/dataset once again the modifications which are doing on grid will be lost. Grid will freshly rebind the rows and columns which are in table/dataset. if we are doing on grid directly we need to find the exact control and need to update that one.
I dont know in which process you r doing. So i have given like that.i think its better to add item template with lable control to your grid as bellow

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="111px"
OnSelectedIndexChanged="GridView1_SelectedIndexCha nged">
<Columns>
<asp:CommandField ButtonType="Button" ShowSelectButton="True" />

<asp:TemplateField HeaderText="time label">
<ItemTemplate>
<asp:Label ID="lbltime" runat="server" />
</ItemTemplate>
</asp:TemplateField>


on selected index change of grid find that control and update that control as bellow . Dont bind grid again with table/dataset. Bcoz it will lose the data.

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
Label lblTime = ((Label)GridView1.Rows[GridView1.SelectedIndex].Cells[1].FindControl("lbltime"));
//Here Cells[1] is 2nd column "time label"
lblTime.Text = DateTime.Now.ToString();
}
Sep 22 '08 #9

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

Similar topics

3
by: JoeK | last post by:
Hey all, I am automating a web page from Visual Foxpro. I can control all the textboxes, radio buttons, and command buttons using syntax such as: ...
0
by: Jane Hawkins | last post by:
Here's my basic need: 1. User clicks on a button 2. Button click event causes some additional controls to appear. 3. User is positioned at start of those additional controls when the page...
1
by: Novice | last post by:
Hey all, I realize this must be a fairly common requirement in ASP.NET but I have yet to come across the right way to do it. Basically, right now I have a single asp:label on my webpage and I am...
2
by: Arjen | last post by:
Hi, I get this error message when sorting a gridview: The GridView 'GridView1' fired event Sorting which wasn't handled What do I need to do? Thanks!
1
by: keithb | last post by:
I need help connecting an image button in a GridView control to an event handler. Using the OnCommand property results in the following error: Error 3...
11
by: bill | last post by:
I dynamically create buttons and associate them with an event using AddHandler. I want all the button events to fire at one time, when the page is posted, instead of when each button is clicked....
3
by: Rick Shaw | last post by:
Hi, can anyone help? I am fairly new to C# and need help with a functionlity in my app. What I need to accomplish is to be able to enable the SAVE button as soon as the user modifies a data in...
6
by: Marc | last post by:
Hi, I am using the below code which simply allows a single button control to be dragged and dropped anywhere around a form. My problem is that want to move about 100 buttons on the form. Is...
3
by: =?Utf-8?B?cGVsZWdrMQ==?= | last post by:
1)how do i add a button to a grid view (in a column of its own)? 2)how do i change the text that appeaers on the button RowCreated Event ? 3)with which event can i change the text on the button...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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:
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
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.