Template column in GridViewhttp://forums.asp.net/t/1774455.aspx/1?Template+column+in+GridViewTue, 28 Feb 2012 05:59:13 -050017744554853907http://forums.asp.net/p/1774455/4853907.aspx/1?Template+column+in+GridViewTemplate column in GridView <p>How can I add a datatable value to TextBox in GridView on ButtonClick Event?</p> <pre class="prettyprint">protected void btnView_Click(object sender, EventArgs e) { //Read TextBox inside GridView value, calculate //Then Add calculated data to TextBox In GridView DataTable tbl = new DataTable(); tbl.Columns.Add(&quot;Ngay&quot;, Type.GetType(&quot;System.DateTime&quot;)); tbl.Columns.Add(&quot;ChiSo&quot;, Type.GetType(&quot;System.Double&quot;)); tbl.Columns.Add(&quot;SanLuong&quot;, Type.GetType(&quot;System.Double&quot;)); tbl.Columns.Add(&quot;GhiChu&quot;, Type.GetType(&quot;System.String&quot;)); tbl.Columns.Add(&quot;Ack&quot;,Type.GetType(&quot;System.Boolean&quot;)); tbl.Columns.Add(&quot;ThoaThuan&quot;, Type.GetType(&quot;System.Boolean&quot;)); tbl.Columns.Add(&quot;Offset&quot;, Type.GetType(&quot;System.Boolean&quot;)); foreach (GridDataItem dataItem in Grid1.Items) { //read data from GridView, worked EnDataNhapTay data = new EnDataNhapTay(); TextBox TextBoxNgay = (TextBox)(dataItem[&quot;Ngay&quot;].FindControl(&quot;txtNgay&quot;)); TextBox TextBoxChiSo = (TextBox)(dataItem[&quot;ChiSo&quot;].FindControl(&quot;txtChiSo&quot;)); TextBox TextBoxSanLuong = (TextBox)(dataItem[&quot;SanLuong&quot;].FindControl(&quot;txtSanLuong&quot;)); TextBox TextBoxGhiChu = (TextBox)(dataItem[&quot;GhiChu&quot;].FindControl(&quot;txtGhiChu&quot;)); CheckBox CheckBoxThoaThuan = (CheckBox)(dataItem[&quot;ThoaThuan&quot;].FindControl(&quot;chkThoaThuan&quot;)); CheckBox CheckBoxOffset = (CheckBox)(dataItem[&quot;Offset&quot;].FindControl(&quot;chkOffset&quot;)); if (string.IsNullOrEmpty(TextBoxChiSo.Text) &amp;&amp; string.IsNullOrEmpty(TextBoxSanLuong.Text)) continue; try { data.DateTime = DateTime.Parse(TextBoxNgay.Text, culture); } catch { continue; } if (string.IsNullOrEmpty(TextBoxChiSo.Text)) data.ChiSo = double.MinValue; else data.ChiSo = double.Parse(TextBoxChiSo.Text); if (string.IsNullOrEmpty(TextBoxSanLuong.Text)) data.SanLuong = double.MinValue; data.ThoaThuan = CheckBoxThoaThuan.Checked; data.OffSet = CheckBoxOffset.Checked; data.GhiChu = TextBoxGhiChu.Text; data.MaVT = cbbMaVT.Text; if (string.IsNullOrEmpty(cbbMaVT.Text)) continue; //Calculate data, worked SqlDataReader rdr = BLMeterData.CalculateMeterData(data); while (rdr.Read()) { DataRow row = tbl.NewRow(); row[0] = (DateTime)rdr[1]; if (DBNull.Value.Equals(rdr[2])) row[1] = DBNull.Value; else row[1] = (double)rdr[2]; if (DBNull.Value.Equals(rdr[3])) row[2] = DBNull.Value; else row[2] = (double)rdr[3]; if (DBNull.Value.Equals(rdr[4])) row[3] = DBNull.Value; else row[3] = (string)rdr[4]; row[4] = (bool)rdr[5]; row[5] = (bool)rdr[6]; row[6] = (bool)rdr[7]; tbl.Rows.Add(row); } Public.CloseDataReader(rdr); } //Write calculated data to GridView, can't see any change when code done??? for (int i = 0; i &lt; tbl.Rows.Count; i&#43;&#43;) { GridDataItem dataItem = Grid1.Items[i]; TextBox TextBoxNgay = (TextBox)(dataItem[&quot;Ngay&quot;].FindControl(&quot;txtNgay&quot;)); TextBox TextBoxChiSo = (TextBox)(dataItem[&quot;ChiSo&quot;].FindControl(&quot;txtChiSo&quot;)); TextBox TextBoxSanLuong = (TextBox)(dataItem[&quot;SanLuong&quot;].FindControl(&quot;txtSanLuong&quot;)); TextBox TextBoxGhiChu = (TextBox)(dataItem[&quot;GhiChu&quot;].FindControl(&quot;txtGhiChu&quot;)); CheckBox CheckBoxAck = (CheckBox)(dataItem[&quot;Ack&quot;].FindControl(&quot;chkGhiChu&quot;)); CheckBox CheckBoxThoaThuan = (CheckBox)(dataItem[&quot;ThoaThuan&quot;].FindControl(&quot;chkThoaThuan&quot;)); CheckBox CheckBoxOffset = (CheckBox)(dataItem[&quot;Offset&quot;].FindControl(&quot;chkOffset&quot;)); TextBoxNgay.Text = ((DateTime)tbl.Rows[i][0]).ToString(dateFormat); if (DBNull.Value.Equals(tbl.Rows[i][1])) TextBoxChiSo.Text = string.Empty; else TextBoxChiSo.Text = ((double)tbl.Rows[i][1]).ToString(); if (DBNull.Value.Equals(tbl.Rows[i][2])) TextBoxSanLuong.Text = string.Empty; else TextBoxSanLuong.Text = ((double)tbl.Rows[i][2]).ToString(); if (DBNull.Value.Equals(tbl.Rows[i][3])) TextBoxGhiChu.Text = string.Empty; else TextBoxGhiChu.Text = ((double)tbl.Rows[i][3]).ToString(); //CheckBoxAck.Checked = (bool)tbl.Rows[i][4]; //CheckBoxThoaThuan.Checked = (bool)tbl.Rows[i][5]; //CheckBoxOffset.Checked = (bool)tbl.Rows[i][6]; } }</pre> <pre class="prettyprint">Thanks.</pre> 2012-02-28T03:27:19-05:004853973http://forums.asp.net/p/1774455/4853973.aspx/1?Re+Template+column+in+GridViewRe: Template column in GridView <p>Hi</p> <p>Cannot get What do you do . Why don't you bind your gridview to your Data table.</p> <p>and bind Textbox in Templatefiled&nbsp; to the required field?</p> <p>B</p> <p>&nbsp;</p> 2012-02-28T04:36:49-05:004854078http://forums.asp.net/p/1774455/4854078.aspx/1?Re+Template+column+in+GridViewRe: Template column in GridView <p>I have just removed update panel wrap gridview, and it worked&nbsp;</p> <p>Dont know why</p> <p>Thank you anyway<img src="http://forums.asp.net/scripts/tiny_mce/plugins/emotions/img/smiley-smile.gif" alt="Smile" title="Smile" border="0"></p> 2012-02-28T05:47:34-05:004854097http://forums.asp.net/p/1774455/4854097.aspx/1?Re+Template+column+in+GridViewRe: Template column in GridView <p>Hi,</p> <p>When you remove Update Panel then actually your page post back completely . before this it was partially postback on button click. so if you put button and gridview in same update panel then it should works or if you put gridview and button in diffrent update panel then set the attribute updatemode = &quot;Conditional&quot;.</p> <p>Regards</p> <p>Shwetamber</p> <p></p> 2012-02-28T05:59:13-05:00