hi..
Simple Gridview Webpart for Sharepoint
Open Visual Studio.....
Click
on New >> Project >> and following screen will appear there
select "Server controll" give any name you want to give :
then in the solution explorer 1 class file will be created give any suitable name to the class file ; here create a public class for the WebPart and then write following code to create a TextBox inside a Webpart where we will write our Queries to populate the Grid:
The pseudocode for that is as follows:
private string sqlquery;
[Personalizable(), WebBrowsable(true),WebDisplayName("SQLQuery"),Category("Data Properties")]
public string SQLQuery
{
get { return sqlquery; }
set { sqlquery = value; }
}
Here
in the code above you can see the Text called as "Data Properties" that
is the section for the name ; you can give what ever name you want for
that section.
Now will create a childcontrol for this.
protected override void CreateChildControls()
{
base.CreateChildControls();
try
{
GridView grv = new GridView();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
string strConnection = System.Configuration.ConfigurationSettings.AppSettings.Get("connection");
string sql = sqlquery;
//DataTable dt = new DataTable();
//dt = objDB.GetSQlResult(sql);
da = new SqlDataAdapter(sql, strConnection);
ds.Clear();
da.Fill(ds, "data");
Controls.Add(grv);
grv.DataSource = ds.Tables[0].DefaultView;
grv.GridLines = GridLines.Horizontal;
grv.CellPadding = 6;
grv.DataBind();
grv.HeaderStyle.BackColor = System.Drawing.Color.LightGray;
}
catch (Exception ex)
{}
}
this
code will create the Gridview webpart now you can build the solution
and you can give the output path of you build to the Bin folder of the
sharepoint site or you create a dll with the strogn name.
1. double click on the properties >> signing and it will allow you to give the string name for that.
2. After
that deploy that dll to the Windows >> Assembly and then
right-click the dll and have a look at the properties there you will
find PublicKeyToken.
3. Then go to the Sharepoint's Web.Config file and register this control as Safecontrol it will look something like this:
Here
Assembly name basically name of the dll , then version and
PublicKeyToken all the information you will find while you will
right-click the dll , namespace is the detail you will see inside the
.CS file. other are the default values.
Now open the sharepoint site and go to the Site settings >> Web parts >> New
There you will find your web part with the extension (webpart) and checked it and click on the
populate gallery.
To check the webpart you create a
new web part page and then click on the
add web part , it will
open a box there you will see
your web part. add it to your page it will look something like this:

Here you will have an option of Edit the web part click on that and you
will a contex menu on side there you will be able to see the "Data
Propertie" under that you will find textbox ; there you can write your
Query. and accordingly Data will get populated.

hope this helps you...