Answered by:
GRIDVIEW problem

Question
-
I want to make a column of gridview be linked button
So I used this code
internal void search(DataGridView dgv, string searchword,int flag) { DataGridViewLinkColumn _colPatientName = new DataGridViewLinkColumn(); _colPatientName.DataPropertyName = "PatientName"; _colPatientName.HeaderText = "Comming Visiting Date"; _colPatientName.Name = "PName"; dgv.Columns.Add(_colPatientName); dgv.DataSource= SearchResult(searchword, flag); } internal DataTable SearchResult(string SWord, int Flag) { using (SqlConnection conn = new SqlConnection(connectiopnstring)) { conn.Open(); SqlCommand command = conn.CreateCommand(); command.CommandType = CommandType.StoredProcedure; SqlParameter parameter1 = command.Parameters.Add("@SearchWord", SqlDbType.NVarChar, 20); parameter1.Direction = ParameterDirection.Input; SqlParameter parameter2 = command.Parameters.Add("@Flag", SqlDbType.SmallInt, 16); parameter2.Direction = ParameterDirection.Input; parameter1.Value = SWord; parameter2.Value = Flag; command.CommandText = "Search"; SqlDataAdapter sdr = new SqlDataAdapter(command); DataTable dt = new DataTable(); sdr.Fill(dt); return dt; } }
the Search stored procedure code is :
ALTER proc [dbo].[Search] @SearchWord nvarchar(20), @Flag int as begin set nocount on declare @x nvarchar(200) if(@flag = 1) select PatientID ,PatientName,convert(varchar(10),NextvistDate,111) as [last_visit_date] from dbo.PatientMain where cast(PatientID as int)=@SearchWord else begin set @x = 'select PatientID ,PatientName,convert(varchar(10),NextvistDate,111) as [last_visit_date] from dbo.PatientMain where PatientName LIKE '''+ '%' + @SearchWord + '%' + '''' EXECUTE sp_executesql @x end end
the problem is the gridview wheb being filled it display wrong columns , with wrong headeer name , also when click the button again it add a new linked column
How to adjust the code such that only 3 columns appear with the correct data
Best regardsSaturday, September 26, 2009 1:06 AM
Answers
-
Hi,
As I learnt from your post , the PatientName column is displayed first. You might want to change DisplayIndex of these column to show them in desired order.
dgv.Columns["PatientID"].DisplayIndex=0;
dgv.Columns["PatientName"].DisplayIndex=1;
For the duplicated column , you might need to decare a public column and check if it's null then add or do not add.
Here is a exmple you can test it:
public DataGridViewLinkColumn _colPatientName;
internal void search()
{
ds = new DataSet();
dt = new DataTable();
dt.Columns.Add("Text1");
dt.Columns.Add("Text2");
ds.Tables.Add(dt);dt.Rows.Add("Test1", "Test1");
dt.Rows.Add("Test1", "error");
dt.Rows.Add("Test1", "fail");
dt.Rows.Add("Test2", "Test2");
if (_colPatientName == null)
{
_colPatientName = new DataGridViewLinkColumn();
_colPatientName.DataPropertyName = "Text2";
_colPatientName.HeaderText = "text2";
this.dataGridView1.Columns.Add(_colPatientName);
}this.dataGridView1.DataSource = dt;
this.dataGridView1.Columns["Text1"].DisplayIndex = 0;
}
Harry
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.Tuesday, September 29, 2009 5:12 AM
All replies
-
Hi,
Are you using a GridView = Web or a DataGridView = windowsforms?
I see gridview wheb in the message
Success
CorSaturday, September 26, 2009 7:02 AM -
Hi,
Are you using a GridView = Web or a DataGridView = windowsforms?
I see gridview wheb in the message
Success
Cor
DataGridView = windowsformsSaturday, September 26, 2009 11:55 AM -
Aabdellatif,
We have to use your eyes to solve a probem you can not solve.
Text as wrong columns , with wrong header name makes that extremely difficult.
Simply telling which columname is a little bit more work for you but less then the effort I took already for your problem.
Success
CorSunday, September 27, 2009 8:08 AM -
Aabdellatif,
We have to use your eyes to solve a probem you can not solve.
Text as wrong columns , with wrong header name makes that extremely difficult.
Simply telling which columname is a little bit more work for you but less then the effort I took already for your problem.
Success
Cor
I can't understand youSunday, September 27, 2009 9:53 AM -
Hi,
As I learnt from your post , the PatientName column is displayed first. You might want to change DisplayIndex of these column to show them in desired order.
dgv.Columns["PatientID"].DisplayIndex=0;
dgv.Columns["PatientName"].DisplayIndex=1;
For the duplicated column , you might need to decare a public column and check if it's null then add or do not add.
Here is a exmple you can test it:
public DataGridViewLinkColumn _colPatientName;
internal void search()
{
ds = new DataSet();
dt = new DataTable();
dt.Columns.Add("Text1");
dt.Columns.Add("Text2");
ds.Tables.Add(dt);dt.Rows.Add("Test1", "Test1");
dt.Rows.Add("Test1", "error");
dt.Rows.Add("Test1", "fail");
dt.Rows.Add("Test2", "Test2");
if (_colPatientName == null)
{
_colPatientName = new DataGridViewLinkColumn();
_colPatientName.DataPropertyName = "Text2";
_colPatientName.HeaderText = "text2";
this.dataGridView1.Columns.Add(_colPatientName);
}this.dataGridView1.DataSource = dt;
this.dataGridView1.Columns["Text1"].DisplayIndex = 0;
}
Harry
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.Tuesday, September 29, 2009 5:12 AM