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...
Where to write code for button event handler in a GridView
Collapse
X
-
i am giving sample code impliment it in your own way
add this in aspx
Code:<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(); } -
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......
Originally posted by adityakoppolui am giving sample code impliment it in your own way
add this in aspx
<asp:GridView ID="GridView1" runat="server" AutoGenerateCol umns="False" Width="111px" >
<Columns>
<asp:ButtonFiel d ButtonType="But ton" Text="Button" />
<asp:BoundFie ld HeaderText="Tim e" DataField="Time " />
</Columns>
</asp:GridView>
add this in aspx.cs page
protected void Page_Load(objec t sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add( "Time");
DataRow dr = dt.NewRow();
dr["Time"] = DateTime.Now.To String();
dt.Rows.Add(dr) ;
GridView1.DataS ource = dt;
GridView1.DataB ind();
}Comment
-
Tyr with this
in aspx
in aspx.cs pageCode:<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="111px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"> <Columns> <asp:CommandField ButtonType="Button" ShowSelectButton="True" /> <asp:BoundField HeaderText="Time" DataField="Time" /> </Columns> </asp:GridView>
Code: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; }Comment
-
i dont understand y u always go for a datatable.....i magine 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...
Originally posted by adityakoppoluTyr with this
in aspx
<asp:GridView ID="GridView1" runat="server" AutoGenerateCol umns="False" Width="111px"
OnSelectedIndex Changed="GridVi ew1_SelectedInd exChanged">
<Columns>
<asp:CommandFie ld ButtonType="But ton" ShowSelectButto n="True" />
<asp:BoundFie ld HeaderText="Tim e" DataField="Time " />
</Columns>
</asp:GridView>
in aspx.cs page
protected void Page_Load(objec t 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.To String();
dt.Rows.Add(dr) ;
DataRow dr1 = dt.NewRow();
dr1["Time"] = DateTime.Now.To String();
dt.Rows.Add(dr1 );
DataRow dr2 = dt.NewRow();
dr2["Time"] = DateTime.Now.To String();
dt.Rows.Add(dr2 );
GridView1.DataS ource = dt;
GridView1.DataB ind();
ViewState["table"] = dt;
}
}
protected void GridView1_Selec tedIndexChanged (object sender, EventArgs e)
{
DataTable dtGV = (DataTable)View State["table"];
dtGV.Rows[GridView1.Selec tedIndex]["Time"] = DateTime.Now.To String();
dtGV.Rows[GridView1.Selec tedIndex].AcceptChanges( );
GridView1.DataS ource = dtGV;
GridView1.DataB ind();
ViewState["table"] = dtGV;
}Comment
-
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 withComment
-
Defaultly added
Defaultly added.......... ............... ............... ....Comment
-
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
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.Code:<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="111px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"> <Columns> <asp:CommandField ButtonType="Button" ShowSelectButton="True" /> <asp:TemplateField HeaderText="time label"> <ItemTemplate> <asp:Label ID="lbltime" runat="server" /> </ItemTemplate> </asp:TemplateField>
Code: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(); }Comment
-
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..
Originally posted by adityakoppoluTo 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" AutoGenerateCol umns="False" Width="111px"
OnSelectedIndex Changed="GridVi ew1_SelectedInd exChanged">
<Columns>
<asp:CommandFie ld ButtonType="But ton" ShowSelectButto n="True" />
<asp:TemplateFi eld HeaderText="tim e label">
<ItemTemplate >
<asp:Label ID="lbltime" runat="server" />
</ItemTemplate>
</asp:TemplateFie ld>
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_Selec tedIndexChanged (object sender, EventArgs e)
{
Label lblTime = ((Label)GridVie w1.Rows[GridView1.Selec tedIndex].Cells[1].FindControl("l bltime"));
//Here Cells[1] is 2nd column "time label"
lblTime.Text = DateTime.Now.To String();
}Comment
Comment