Help On Grid View Binding

Posted by Vijayar under C# on 9/8/2011 | Points: 10 | Views : 1556 | Status : [Member] | Replies : 9
Hi
i have 2 tables countries and states, i am using a query to get all states of the country under it,but my problem is i need to bind the same data to the grid view,i am using using union query to get the desired result.But inow iam not getting idea how to bind it to grid view using Eval

query is
SqlDataAdapter da = new SqlDataAdapter(" Select 'C'+Cast(countryID as varchar)id , CountryID as CountryID,CountryName from Countries UNION Select 'S'+Cast(stateID as varchar)stateID ,CountryID,StateName from [States] order by CountryID,ID", conn);

o/p
India
-------------------------------------------------------------------
Ap
-------------------------------------------------------------------
Tamil Nadu
--------------------------------

UsA



should be shown in gridview.Please help me in design part also

vijaya


Responses

Posted by: Vuyiswamb on: 9/8/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
ok what is exactly your problem ? Does your Union when ran in SQL Brings data ? please explain clearly

Thank you for posting at Dotnetfunda
[Administrator]

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

Posted by: Vijayar on: 9/8/2011 [Member] Starter | Points: 25

Up
0
Down
yes,when i run the query i am getting the data as required in sql.But when i am unable to bind the same query to grid view.Help Me in this issue
how to bind it to grid view.Explain me to get in above format


vijaya

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

Posted by: Vuyiswamb on: 9/8/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
Let me see your C# code that you attempted to write

Thank you for posting at Dotnetfunda
[Administrator]

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

Posted by: Vijayar on: 9/8/2011 [Member] Starter | Points: 25

Up
0
Down
private void BindGridView()
{

try
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings[""].ToString();

conn.Open();
//SqlDataAdapter da = new SqlDataAdapter("SELECT * From MailTemplate ", conn);

SqlDataAdapter da = new SqlDataAdapter("Select 'MC'+Cast(CategoryId as varchar)id , CategoryId as CategoryId,CategoryName from MailCategory UNION Select 'S'+Cast(TemplateId as varchar)tempID ,MailTempCategory,MailTempName from MailTemplate order by CategoryId,ID", conn);
DataSet ds = new DataSet();
da.Fill(ds);


gvmailtemp.DataSource = ds;
gvmailtemp.DataBind();
}
catch (Exception ex)
{
ex.Message.ToString();
}
}


vijaya

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

Posted by: Majith on: 9/8/2011 [Member] Starter | Points: 25

Up
0
Down
Greetings,

Could you please share the table structure. please dont use the inline query as it impacts the performance instead you
can use strored procedure. it is best practice too.

Thanks,



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

Posted by: Vuyiswamb on: 9/8/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
Can i see the html of your gridview

Thanks

Thank you for posting at Dotnetfunda
[Administrator]

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

Posted by: Vijayar on: 9/8/2011 [Member] Starter | Points: 25

Up
0
Down
<asp:GridView ID="gvmailtemp" runat="server" AutoGenerateColumns="False" Width="100%"
AllowPaging="True" OnRowCommand="gvmailtemp_RowCommand" CssClass="datatable"
CellPadding="0" CellSpacing="0" BorderWidth="0" GridLines="None">
<Columns>
<asp:TemplateField HeaderText="S.No" HeaderStyle-CssClass="first" ItemStyle-CssClass="first">
<ItemTemplate>
<%# Container.DataItemIndex+1 %>
<asp:Label ID="lblSerialNo" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" Visible="false">
</asp:BoundField>
<asp:BoundField DataField="MailTempName" HeaderText=" Mail Temp Name" Visible="false">
</asp:BoundField>
<asp:TemplateField HeaderText="categoryname/mail temp name">
<ItemTemplate>

<asp:Label ID="Label1" runat="server" Text="">
<%# Eval("CategoryName") + "<br> " + "____________" + "<br>" + Eval("MailTempName")%>
</asp:Label>
</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Edit" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:ImageButton ID="Edit" runat="server" Text="Edit" ForeColor="#000066" CausesValidation="false"
CommandName="MyEdit" CommandArgument='<%#Eval("TemplateId") %>' ImageUrl="~/Pages/images/1303308225_edit.png"
ToolTip="Edit" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:ImageButton ID="Delete" runat="server" Text="Delete" ForeColor="#000066" CausesValidation="false"
CommandName="MyDelete" OnClientClick="return confirm('Are you sure you want to delete Template?') "
CommandArgument='<%#Eval("TemplateId") %>' ImageUrl="~/Pages/images/1303308433_edit-delete.png"
ToolTip="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


Help Me in designing grid view also

vijaya

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

Posted by: Vijayar on: 9/8/2011 [Member] Starter | Points: 25

Up
0
Down
Mail Category Table

categoryid,categoryname

mailTemplate------------------TemplateId
[MailTempCategory]
[MailTempName]

[MailMessage] ,the common field is categoryid(the name in another table is mailtempcategory)

vijaya

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

Posted by: Vuyiswamb on: 9/8/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
For now lets test this , change your gridview to look like this and test if there are any records in the grid when you run it

<asp:GridView ID="gvmailtemp" runat="server" AutoGenerateColumns="true" Width="100%" 

AllowPaging="True" OnRowCommand="gvmailtemp_RowCommand" CssClass="datatable"
CellPadding="0" CellSpacing="0" BorderWidth="0" GridLines="None">

</asp:GridView>


Thank you for posting at Dotnetfunda
[Administrator]

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

Login to post response