Dynamic Gridview Column Editing [Resolved]

Posted by Rasagna under ASP.NET on 12/21/2015 | Points: 10 | Views : 8607 | Status : [Member] | Replies : 4
I have to display data in a gridview dynamically that is without using standard datasource from gridview.So I have used dataset to populate and display data in dynamic gridview.
Code in .aspx file
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView2" runat="server">
</asp:GridView>
</div>
</form>

Code in .aspx.cs file
protected void Page_Load(object sender, EventArgs e)
{
string strcon = ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString;
SqlConnection con = new SqlConnection(strcon);
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [EmpInfo2]", con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView2.DataSource = ds;
GridView2.DataBind();
}

I have a column as Gender with datatype as bit(stores true or false) in SQL database.So when we obtain data in gridview in Gender column checkbox is displayed either checked or unchecked.but i want to display Male or Female(Say for checked it must display male for unchecked it must display female) instead of checkbox.I know that by using template databound fields in standard gridview it can be achieved but here it is dynamic where no databound field can be edited or templated.what can be done?




Responses

Posted by: Rajnilari2015 on: 12/22/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
1
Down

Resolved
@Rasagna Sir, Ok now I got your point. so in that case you need to do this small change

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)

{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var ctrl = (e.Row.Cells[2].Controls[0] as CheckBox);
ctrl.Text = ctrl.Checked? "Male" : "Female";
}
}


Hope this helps

--
Thanks & Regards,
RNA Team

Rasagna, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rasagna on: 12/22/2015 [Member] Starter | Points: 25

Up
1
Down

Resolved
There is also another way to deal with Dynamic Gridviews i.e. by making changes in sql statement itself like below http://www.dotnetfunda.com/forums/show/20921/query-for-casting-bit-datatype-to-nvarchar

Rasagna, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 12/21/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
1
Down
@Rasagna Sir, if I understand your need properly, may be you are looking for a asp:BoundField and on the OnRowDataBound event you need to do the change.

A working version is presented below (as per my understanding of the question)

GridView HTML Markup

<asp:GridView ID="GridView2" runat="server" HeaderStyle-BackColor="gray"

AutoGenerateColumns="False" BackColor="white"
BorderColor="blue" BorderStyle="None" OnRowDataBound="GridView2_RowDataBound">
<Columns>
<asp:BoundField DataField="UserId" HeaderText="User Id">
<ItemStyle HorizontalAlign="Left" Width="10%" />
</asp:BoundField>
<asp:BoundField DataField="Name" HeaderText="Name">
<ItemStyle HorizontalAlign="Left" Width="20%" />
</asp:BoundField>

<asp:CheckBoxField DataField="Gender" HeaderText="Gender(Your Existing)" />

<asp:BoundField DataField="Gender" HeaderText="Gender(Change at runtime)">
<ItemStyle HorizontalAlign="Left" Width="10%" />
</asp:BoundField>

</Columns>
</asp:GridView>


Code Behind

using System;

using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
GridView2.DataSource = MyDataTable();
GridView2.DataBind();
}

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TableCell cell = e.Row.Cells[3];
cell.Text = cell.Text == "true" ? "Male" : "Female";
}
}

private DataTable MyDataTable()
{
DataTable dtsource = new DataTable();

//Adding columns to datatable
dtsource.Columns.Add("UserId");
dtsource.Columns.Add("Name");
dtsource.Columns.Add("Gender");

//Adding records to the datatable
dtsource.Rows.Add("1", "Name1", "true");
dtsource.Rows.Add("2", "Name2", "true");
dtsource.Rows.Add("3", "Name3", "false");
dtsource.Rows.Add("4", "Name4", "true");
dtsource.Rows.Add("5", "Name5", "false");

return dtsource;
}
}


Hope this helps.
 Download source file

--
Thanks & Regards,
RNA Team

Rasagna, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rasagna on: 12/21/2015 [Member] Starter | Points: 25

Up
0
Down
@ Rajnilari2015, Thanks for the reply
But here I dont want to add any more columns to the gridview as it already contains gender column obtained automatically from dataset.Is there any other way?

Rasagna, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response