C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Fully Editable GridView in using C#


Posted Date: 28 Aug 2008      Total Responses: 1

Posted By: Beladev       Member Level: Bronze     Points: 1


Hi

Hi

I have a gridview which, I binded with a Dataset . This Dataset I am binding to the gridview at run time. So now after this I want to make all the rows in the gridview Editable. So that I can insert all the data ,which have been enterd to the gridview to the database.

Here the code which i am Binding the Gridview to Dataset

#region "Grid View Bind Data"
public void GridBindData()
{
string str;
int count;
MySqlConnection cn = new MySqlConnection(dbconn);
cn.Open();



MySqlDataAdapter ada = new MySqlDataAdapter("SELECT distinct Sub_name from subject_details where Course_ID in(Select Course_ID from Course_details group by Combination='" + ddlcom.SelectedValue + "')", cn);
DataSet ds = new DataSet();
ada.Fill(ds);
count = ds.Tables[0].Rows.Count;

MySqlDataAdapter adaexmid = new MySqlDataAdapter("SELECT Distinct Exam_ID from exam_desc WHERE (Test_desc='" + ddlEdesc.SelectedValue + "')", cn);// && Test_date='" + ddlEdate.SelectedValue + "'
DataSet dsexmid = new DataSet();
adaexmid.Fill(dsexmid);
exmid = (int)dsexmid.Tables[0].Rows[0].ItemArray[0];


MySqlDataAdapter adacou = new MySqlDataAdapter("Select Course_ID,Grp_ID from Course_details where (Course = '" + ddlcourse.SelectedValue + "' && Grp='" + ddlgrp.SelectedValue + "' && Combination='" + ddlcom.SelectedValue + "' && Yer='" + ddlyr.SelectedValue + "')", cn);
DataSet dscou = new DataSet();
adacou.Fill(dscou);
int coursecount = dscou.Tables[0].Rows.Count;
string cour="(";
for (int j = 0; j < coursecount; j++)
{
courseid = (string)dscou.Tables[0].Rows[j].ItemArray[0];
//(Course_ID='PSP127'||Course_ID='PSP129'||Course_ID='PSP130')
string couid1 = "Course_ID=" + "'\""+ "+" + courseid + "+"+"\"'||";
cour = cour + couid1;
}
cour=cour.Substring(0,cour.Length-2);

cour.Replace("'\'", "");

grpid = (string)dscou.Tables[0].Rows[0].ItemArray[1];

string studename = "Select Stud_ID,Stud_fname,Reg_no from student_master where (Course_ID='PSP127')";
MySqlDataAdapter adacou1 = new MySqlDataAdapter(studename, cn);
DataSet adacou11 = new DataSet();
adacou1.Fill(adacou11);




BoundField b2 = new BoundField();
b2.HeaderText = "Reg No";
b2.ReadOnly = true;
//b2.ControlStyle.Width = 0;
b2.Visible = true;
b2.DataField = adacou11.Tables[0].Columns[2].ColumnName;
GridView1.Columns.Add(b2);


BoundField b3 = new BoundField();
b3.HeaderText = "Student ID";
b3.ReadOnly = false;
b3.ControlStyle.Width = 0;
b3.Visible = true;
b3.DataField = adacou11.Tables[0].Columns[0].ColumnName;
GridView1.Columns.Add(b3);

BoundField b4 = new BoundField();
b4.HeaderText = "Student Name";
b4.ReadOnly = true;
b4.ControlStyle.Width = 0;
b4.Visible = true;
b4.DataField = adacou11.Tables[0].Columns[1].ColumnName;
GridView1.Columns.Add(b4);


for (int i = 1; i <= count; i++)
{

str = (string)ds.Tables[0].Rows[i - 1].ItemArray[0];
b5.HeaderText = str;

GridView1.Columns.Add(b5);
}



CommandField ecol1 = new CommandField();
ecol1.ShowEditButton = true;
ecol1.EditText = "Edit";
GridView1.Columns.Add(ecol1);




GridView1.DataSource = adacou11;
GridView1.DataBind();


ds = null;


cn.Close();

}

#endregion


Plz Send me Some solution..


My Frirst Question Is

Is it possible?




Responses

Author: ANIL PANDEY    29 Aug 2008Member Level: DiamondRating:     Points: 6
hi,

try this code...


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Xml;
using System.IO;


public partial class PersonalInfo : System.Web.UI.Page
{
SqlConnection con;
SqlDataAdapter sqlAda;
DataSet lds;

protected void Page_Load(object sender, EventArgs e)
{
lblMessage.Text = "";
// Opening the Connection
Connect();
createXML();
if (!Page.IsPostBack)
{
Bind();
}
}
protected void btnSave_Click(object sender, EventArgs e)
{

if (txtName.Text == "" || txtEmail.Text == "")
{
lblMessage.Text = "Please Enter the Required Values...";

}
else
{

string lstrQuery = "insert into info values('" + txtName.Text + "','" + txtAdd.Text + "','" + txtPhone.Text + "','" + txtComp.Text + "','" + txtEmail.Text + "')";
SqlCommand cmd = new SqlCommand(lstrQuery, con);
int i = cmd.ExecuteNonQuery();
if (i == 1)
{
lblMessage.Text = "Record Succesfully Inserted...";
txtName.Text = "";
txtAdd.Text = "";
txtPhone.Text = "";
txtComp.Text = "";
txtEmail.Text = "";
txtName.Focus();
}
else
{
lblMessage.Text = "Record Not Inserted...";
}

//binding the Grid

Bind();
}
}

public void Bind()
{
sqlAda = new SqlDataAdapter("select * from info",con);
lds = new DataSet();
sqlAda.Fill(lds);
grdDetails.DataSource = lds.Tables[0];
grdDetails.DataBind();
}

public void Connect()
{
string strConnStr = "Data Source=GP-182\\SQLEXPRESS;Database=Sample;Integrated Security=true;";
con = new SqlConnection(strConnStr);
con.Open();
}
protected void grdDetails_SelectedIndexChanged(object sender, EventArgs e)
{

}
protected void grdDetails_RowEditing(object sender, GridViewEditEventArgs e)
{
grdDetails.EditIndex = e.NewEditIndex;
Bind();
}
protected void grdDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int lintDataKey;
TextBox txtName1;
TextBox txtAdd1;
TextBox txtPhone1;
TextBox txtComp1;
TextBox txtEmail1;

lintDataKey= Convert.ToInt32( grdDetails.DataKeys[e.RowIndex].Value);

txtName1=(TextBox)grdDetails.Rows[e.RowIndex].Cells[0].Controls[0];
txtAdd1 = (TextBox)grdDetails.Rows[e.RowIndex].Cells[1].Controls[0];
txtPhone1 = (TextBox)grdDetails.Rows[e.RowIndex].Cells[2].Controls[0];
txtComp1 = (TextBox)grdDetails.Rows[e.RowIndex].Cells[3].Controls[0];
txtEmail1 = (TextBox)grdDetails.Rows[e.RowIndex].Cells[4].Controls[0];

string lstrQuery = "Update info set Name='" + txtName1.Text + "',Address='" + txtAdd1.Text + "',Phone='" + txtPhone1.Text + "',Compnay='" + txtComp1.Text + "',email='" + txtEmail1.Text + "' where Sno="+ lintDataKey +"";
SqlCommand cmd = new SqlCommand(lstrQuery, con);
int i = cmd.ExecuteNonQuery();
if (i == 1)
{
lblMessage.Text = "Record Updated Succesfully...";
}
grdDetails.EditIndex = -1;
Bind();
}
protected void grdDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
grdDetails.EditIndex = e.RowIndex;
Bind();
}
protected void grdDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grdDetails.EditIndex = -1;
Bind();
}
protected void btnCancel_Click(object sender, EventArgs e)
{
txtName.Text = "";
txtAdd.Text = "";
txtPhone.Text = "";
txtComp.Text = "";
txtEmail.Text = "";
txtName.Focus();
}



public void createXML()
{
XmlDocument doc = new XmlDocument();


string filename="c:\\sample.xml";
XmlTextWriter tw=new XmlTextWriter(filename,null);//null represents the Encoding Type//
tw.Formatting=Formatting.Indented; //for xml tags to be indented//

//tw.WriteStartDocument(); //Indicates the starting of document (Required)//


//tw.WriteStartElement("Employees");
//tw.WriteStartElement("Employee","Genius");
//tw.WriteAttributeString("Name","krishnan");
//tw.WriteElementString("Designation","Software Developer");
//tw.WriteElementString("FullName","krishnan Lakshmipuram Narayanan");

tw.WriteStartElement("Author");
tw.WriteElementString("ID","1");
tw.WriteElementString("SubmissionDate","1-1-08");
tw.WriteElementString("Topic","test");
tw.WriteElementString("Title","test");
tw.WriteStartElement("Authors");
tw.WriteStartElement("Author");
tw.WriteElementString("Name","XYZ");
tw.WriteElementString("Affiliation","ABC");
tw.WriteEndElement();
tw.WriteEndElement();
tw.WriteElementString("Summary","Hi");
tw.WriteElementString("Text","Hello");

tw.WriteEndElement();
//tw.WriteEndDocument();
tw.Flush();
tw.Close();

StreamReader srXml = new StreamReader(filename);
string xmlFile = srXml.ReadToEnd();



}
}


Thanks
Anil



Post Reply
You must Sign In to post a response.
Next : alert message problem
Previous : gridview
Return to Discussion Forum
Post New Message
Category: .NET

Related Messages



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use