SeethaPartha

SeethaPartha

  • NA
  • 89
  • 29.9k

A column data of DB in grid view as 2 Columns

Nov 17 2017 2:27 AM
How to bind a particular database field in Grid view as 2 columns.
 
As usual all data added row wise only.
1
2
3
4
5
6
7
8
9
10 
 
I need that in 2 column like 
 
1   2               1   6
3   4               2   7
5   6      or      3   8
7   8               4   9
9   10             5   10
 
 
so far i have used like below. Please suggest to change this code based on above requirement.
 
 
<asp:GridView ID="GridCatg" runat="server" AutoGenerateColumns="False" DataKeyNames="HCM_CATG_CD"
OnRowDataBound="GridCatg_RowDataBound" PageIndex="3" PageSize="10">
<Columns>
<asp:TemplateField HeaderText="CATEGORY AND ITS FORMS">
<ItemTemplate>
<asp:CheckBox ID="cbAll" runat="server" Font-Bold="true"/>
<asp:CheckBoxList ID="cb_CATG_FORM" runat="server" Visible="True"
AutoPostBack="False" Height="40px">
</asp:CheckBoxList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
 
private void BindGrid()
{
DataSet ds = new DataSet();
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager
.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select * from AS_CATG_MASTER";
cmd.Connection = conn;
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(ds);
GridCatg.DataSource = ds;
GridCatg.DataBind();
conn.Close();
}
}
}
protected void GridCatg_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Binding checkbox list to Gridview
if (e.Row.RowIndex != -1 && e.Row.DataItem != null)
{
object objTemp = GridCatg.DataKeys[e.Row.RowIndex].Value as object;
if (objTemp != null)
{
Catgname = objTemp.ToString();
//Do your operations
}
//Itendifying checkboxlist in gridview template column
CheckBox chkall = (CheckBox)e.Row.Cells[0].FindControl("cbAll");
chkall.Text = Catgname;
CheckBoxList chklist = (CheckBoxList)e.Row.Cells[0].FindControl("cb_CATG_FORM");
DataSet bpds = ExecuteDataset(sDsn, "select * from AS_CATG_FORM WHERE HCF_CATG_CD='" + Catgname + "'");
if (bpds.Tables.Count > 0)
{
if (bpds.Tables[0].Rows.Count > 0)
{
//Assigning DataSource to checkboxlist
chklist.DataSource = bpds.Tables[0];
chklist.DataTextField = "HCF_FORM_CD";
chklist.DataValueField = "HCF_FORM_CD";
chklist.DataBind();
//chklist.Items.Insert(0, new ListItem(Catgname));
}
}
chkall.Attributes.Add("onclick", "CallChk('" + chklist.ClientID.ToString() + "','" + chklist.Items.Count + "','" + chkall.ClientID.ToString() + "')");
}
}
 
 
 
 
 

Answers (2)