Connecting Tech Pros Worldwide Forums | Help | Site Map

Where to write code for button event handler in a GridView

Member
 
Join Date: Jun 2007
Posts: 88
#1: Sep 19 '08
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...

Newbie
 
Join Date: Sep 2008
Posts: 17
#2: Sep 19 '08

re: Where to write code for button event handler in a GridView


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.     }
Member
 
Join Date: Jun 2007
Posts: 88
#3: Sep 19 '08

re: Where to write code for button event handler in a GridView


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



Quote:

Originally Posted by adityakoppolu

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();

}

Newbie
 
Join Date: Sep 2008
Posts: 17
#4: Sep 19 '08

re: Where to write code for button event handler in a GridView


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.     }
Member
 
Join Date: Jun 2007
Posts: 88
#5: Sep 19 '08

re: Where to write code for button event handler in a GridView


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






Quote:

Originally Posted by adityakoppolu

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

Newbie
 
Join Date: Sep 2008
Posts: 17
#6: Sep 20 '08

re: Where to write code for button event handler in a GridView


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
Newbie
 
Join Date: Sep 2008
Posts: 17
#7: Sep 20 '08

re: Where to write code for button event handler in a GridView


Defaultly added............................................
Newbie
 
Join Date: Sep 2008
Posts: 17
#8: Sep 20 '08

re: Where to write code for button event handler in a GridView


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. }
Member
 
Join Date: Jun 2007
Posts: 88
#9: Sep 22 '08

re: Where to write code for button event handler in a GridView


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


Quote:

Originally Posted by adityakoppolu

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();
}

Reply