Click here to Skip to main content
15,906,467 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my first application in ASP.Net

The asp:gridview is not visible in the form?
What i have made wrong?

Please advise me.
SwaxRak
Thanks


XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Mobile Shop Cart</title>
</head>
<body>
    <form id="ShopCart" runat="server">
    <div>
        <asp:Label Text="List Of Available Items" runat="server"></asp:Label>
        <asp:GridView ID="Gridview1" runat="server">
            <Columns>
             <asp:CommandField ShowDeleteButton="true" ShowEditButton="true" />
             <asp:BoundField DataField ="ItemID" HeaderText ="Item ID" ReadOnly="true" />
             <asp:BoundField DataField ="ItemDescription" HeaderText ="Item Description" ReadOnly="true"/>
            </Columns>
        </asp:GridView>
        <asp:Button Text="Buy" runat="server" />
    </div>
    </form>
</body>
</html>
Posted

Did you bind any data to the grid?
 
Share this answer
 
Comments
SwaxRak 13-Jul-11 7:11am    
I don't want to enter any data...
I need to show the empty grid view with columns.
I would assume there is no data to be displayed in the grid. I think the GridView control has an EmptyDataText property or similar for this scenario but you'd have to look it up to get the specifics.
 
Share this answer
 
Comments
SwaxRak 13-Jul-11 6:47am    
Can we add the values manually in asp.net as like winforms ?
Have you written code in codebehind ?
first bind data with column ItemID, ItemDescription.
 
Share this answer
 
Comments
SwaxRak 13-Jul-11 7:13am    
I don't want to enter any data... in grid view
I need to show the empty grid view with columns.

I used the answer of goblinTech and I have added the code with EmptyDataText property also.Still no luck.
http://msdn.microsoft.com/en-us/library/aa479342.aspx try this example
 
Share this answer
 
Step 1:

To create Gridview.

Step 2:

Then to Bind the data to grid through to add DataSource or using following c# coding.

con.open();
string query = "Select pk,name,mrpr,discount,sellp,pphoto from prode";
SqlDataAdapter sda = new SqlDataAdapter(query, con);
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.close();
 
Share this answer
 
Comments
SwaxRak 13-Jul-11 7:11am    
I don't want to enter any data...
I need to show the empty grid view with columns.
http://msdn.microsoft.com/hi-in/beginner/default(en-us).aspx


Regard
Hitendra Lariya
 
Share this answer
 
you may try this template
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void groupBox1_Enter(object sender, EventArgs e)
        {
        }
        private void addbutton_Click(object sender, EventArgs e)
        {
            panel2.Hide();
            panel1.Show();
        }
        private void showbutton_Click(object sender, EventArgs e)
        {
            panel1.Hide();
            panel2.Hide();
            //Create connection here
            SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\FASTRACK\Documents\vb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
            string select = "select * from customer";
            SqlCommand cmd = new SqlCommand(select, cn);
            // Connection open here
            cn.Open();
            //create datatable object and use datagrid to show data
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            dataGridView1.DataSource = dt;
            //connection close here
            cn.Close();
        }
        private void button1_Click(object sender, EventArgs e)
        {
             //Create connection here
            
            SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\FASTRACK\Documents\vb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
            string add = "insert into customer(cname,cadd,cmobile) values(@cname,@cadd,@cmobile)";
            SqlCommand cmd = new SqlCommand(add, cn);
            // Connection open here
            cn.Open();
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "")
            {
                errorProvider1.SetError(button1, "No text box should be emty");
               
            }
            else
            {
                //Pass value from text box
                cmd.Parameters.AddWithValue("cname", textBox1.Text);
                cmd.Parameters.AddWithValue("cadd", textBox2.Text);
                cmd.Parameters.AddWithValue("cmobile", textBox3.Text);
                cmd.ExecuteNonQuery();
                //Connection close here
                cn.Close();
                MessageBox.Show("Record Added successfully", "Record Added", MessageBoxButtons.OK);
                panel1.Hide();
            }

        }
        private void deletebutton_Click(object sender, EventArgs e)
        {
            //Create connection here
            SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\FASTRACK\Documents\vb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
            string delete = "delete from customer where cname=@cname";
            SqlCommand cmd = new SqlCommand(delete, cn);
            // Connection open here
            cn.Open();
            //delete row from database
            
        
           
            
            // Connection close here
            cn.Close();
        }
        private void updatebutton_Click(object sender, EventArgs e)
        {
            panel1.Hide();
            panel2.Show();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            //Create connection here
            SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\FASTRACK\Documents\vb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
            string update = "insert into customer(cname,cadd,cmobile) values(@cname,@cadd,@cmobile)";
            SqlCommand cmd = new SqlCommand(update, cn);
            // Connection open here
            cn.Open();
           
           
            if (textBox4.Text == "" || textBox5.Text == "" || textBox6.Text == "")
            {
                errorProvider1.SetError(button1, "No text box should be emty");
            }
            else
            {
                //Pass value from text box
                cmd.Parameters.AddWithValue("cname", textBox4.Text);
                cmd.Parameters.AddWithValue("cadd", textBox5.Text);
                cmd.Parameters.AddWithValue("cmobile", textBox6.Text);
                cmd.ExecuteNonQuery();
                //Connection close here
                cn.Close();
                MessageBox.Show("Record Added successfully", "Record update", MessageBoxButtons.OK);
                panel2.Hide();
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
    }
}
 
Share this answer
 
Comments
SwaxRak 13-Jul-11 7:18am    
I don't want to add any data.... through sql database.
Just empty datagrid view with two columns item id and item description
dksaini14 13-Jul-11 7:20am    
ok in which platform you works visual studio or other than this
SwaxRak 13-Jul-11 7:22am    
Visual Studio 2010
Rhys Gravell 13-Jul-11 7:20am    
If you don't want to add or display any data, why do you need to display a grid at all? Just display a 2x1 table, after all a grid renders as a table structure anyway...
dksaini14 13-Jul-11 7:22am    
@ goblintech
exactly
ok follows the following steps

1. click on data gried view in tool box (ALt Ctrl X) under data categeory

2.click on right top of fried view box

3. you have option to add columns etc.
 
Share this answer
 
Comments
SwaxRak 13-Jul-11 7:32am    
Thank you...........
Solution accepted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900