2

I have added login button to DataGridView programmatically. I want to check field logintime from database and if it is null the button name should be login else it's name should be logout.

private void frmAttendance_Load(object sender, EventArgs e) 
{ 
    GetData();//Fetch data from database 
    DataGridViewButtonColumn buttonLogin = new DataGridViewButtonColumn(); 
     buttonLogin.Name = "Login"; 
    buttonLogin.Text = "Login"; 
    buttonLogin.UseColumnTextForButtonValue = true; 
    dataGridView1.Columns.Add(buttonLogin); 
    // Add a CellClick handler to handle clicks in the button column. 
    dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick); 
}
2
  • Hi, Welcome to SO! Please show us what you have done so far, please read this Commented Oct 16, 2015 at 9:01
  • I edited the answer and now it's enough to use the first part only. To add a button column you can. Use the second part only if you need to set different text for buttons. Hope you find the update helpful :) Commented Nov 20, 2015 at 2:51

2 Answers 2

3

To add a button column you can:

var button=new DataGridViewButtonColumn();
button.Name="LoginButton";
button.HeaderText="Login";
button.Text = "Login";
button.UseColumnTextForButtonValue = true;

this.dataGridView1.Columns.Add(button);

To set text of button column dynamically

To show "Login" text on each button, its enough to set:

button.Text = "Login";
button.UseColumnTextForButtonValue = true;

Also if you need to set different text for buttons, you can use CellFormatting event of DataGridView and Set the value of those cells:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    //If this is header row or new row, do nothing
    if (e.RowIndex < 0 || e.RowIndex == this.dataGridView1.NewRowIndex)
        return;

    //If formatting your desired column, set the value
    if (e.ColumnIndex=this.dataGridView1.Columns["LoginButton"].Index)
    {
        //You can put your dynamic logic here
        //and use different values based on other cell values, for example cell 2
        //this.dataGridView1.Rows[e.RowIndex].Cells[2].Value
        e.Value = "Login";
    }
}

You should assign this handler to CellFormating event:

this.dataGridView1.CellFormatting += dataGridView1_CellFormatting;
Sign up to request clarification or add additional context in comments.

2 Comments

@user3089887 And how can LoginTime be related to How to add button name and text dynamically to gridview button?
@user3089887 you don't need that column to be in dataGridView to access it. For examle you can access it this way in cell formatting event dataTable1.Rows[e.RowIndex][5] , use index of that column in your data table, also the result is object and you can cast it to DateTime or use ToString()
1

You can go hrough the DataGridView in a loop:

foreach(DataGridViewRow row in dataGridView1.Rows)
{
    DataGridViewCell cell = row.Cells[0] //Button column index.
    //Put your data logic here.
    cell.Value = "Login";
}

But in this case, you have to know the column index of your DataGridViewButtonColumn

7 Comments

Very simple and nice, but it doesn't support new added rows and can't be update if you want to set button value based on other cell values while other cell values changes. +1 :)
I am not fetching loginTime in dataGridview so how can do it while loading datagridview.
I think @RezaAghaei answer might suit you more. Since my method can only be run after the datagridview is loaded and you wont change it anymore. Otherwise I need more info from you where you load in loginTime.
@RezaAghaei I don't load loginTime anywhere in dataGridview I can access it throgh datasets while loading datagridview
@user3089887 What's LoginTime?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.