.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> Windows Application >> Post New QuestionBookmark and Share Subscribe to Forum

datagridview --- only 3(10) colums have to be bounded

Posted By: Jayakumar     Posted Date: March 05, 2010    Points:2   Category :Windows Application
hi,

i am working @ windows application with c#.

i have to select 10 columns(single row with 10 columns) from table A.

first--i have to bind only three columns with datagridview1..
second--i have to show out ten columns in separate labels on datagridview1 content click event.

autogenerate column is not working.

how it can be done..

Responses
Author: Syed Shakeer Hussain             
Posted Date: March 06, 2010     Points: 5   

Hi jayakumar,
1)Binding only three column to the datagridview
SqlDataAdapter da=new SqlDataAdapter("select id,name,deptname from A",conn);
DataSet ds=new DataSet();
da.Fill(ds,"A);
DataGridView1.DataSource=ds.Tables["A];


OutPut of your DataGidview

id name deptname

-----------------------
1 AAA dAAA
2 BBB dBBB
3 CCC dCCC

2)bleow code is when you click on the content of you DataGrid it show the corresponding row Values

->Double click the DataGridView and write the below code

Private void dataGridView1_CellContentClick_1(objectsender,DataGridViewCellEventArgs e)
{
i = dataGridView1.SelectedCells[0].RowIndex;
int id= dataGridView1.Rows[i].Cells[0].Value;
SqlCommand cmd=new SqlCommand("select col1,col2,col3,col4,col5,........,col9,col10 where id="+id+"",conn);
SlqDataReader dr=cmd.ExecuteReader();
if(dr.Read())
{
lable11.Text=dr["col1].ToString();
label2.Text=dr["col2].ToString();
:
:
:
:
lable19.Text=dr["col9].ToString();
label10.Text=dr["col10].ToString();
}
else
{
label1.Text="No Records Found";
}

}

Note:In above Code or Program column 'id' have to use to get the values.


Make it as ANSWER if it HELPS you

Author: Jayakumar             
Posted Date: March 07, 2010     Points: 5   

hi

i am getting object ref=null error on dataGridView1_CellContentClick event.

label9.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();

label10.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
label11.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();

label12.Text = dataGridView1.Rows[i].Cells[4].Value.ToString();
label13.Text = dataGridView1.Rows[i].Cells[5].Value.ToString();
label14.Text = dataGridView1.Rows[i].Cells[6].Value.ToString();

label15.Text = dataGridView1.Rows[i].Cells[7].Value.ToString();
label16.Text = dataGridView1.Rows[i].Cells[8].Value.ToString();
label19.Text = dataGridView1.Rows[i].Cells[9].Value.ToString();

label20.Text = dataGridView1.Rows[i].Cells[10].Value.ToString();
textBox1.Text = dataGridView1.Rows[i].Cells[11].Value.ToString();

since i have make visible false for 7 column and binded data with dgv.



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend